How do you get a timestamp in JavaScript?

Learn, what is timestamp, and how to get a timestamp using various methods in JavaScript?
Submitted by Pratishtha Saxena, on May 15, 2022

What is Timestamp?

First, let's understand what is a timestamp? Here, the timestamp is the number of milliseconds that have passed since Jan 1, 1970. This is known as the Unix epoch. So basically, if we print the timestamp it is going to be a long integer as it is the number of milliseconds. It is going to be something like 1652189912918.

1) Using Date.now()

This method will return the current timestamp in milliseconds.

Example 1:

// Creating a timestamp
var timestamp = Date.now();
console.log(timestamp);

Output:

1652624897488

These are the number of milliseconds passed from Jan 1, 1970. To see the number of seconds, we will divide the time by 1000.

Example 2:

// Creating a timestamp
var timestamp = Date.now() / 1000;
console.log(timestamp);

Output:

1652625277.306

To get the floor value of this number we'll use the Math.floor() method. This will round off the given value.

Example 3:

// Creating a timestamp
var timestamp = Math.floor(Date.now() / 1000);
console.log(timestamp);

Output:

1652625459

But this is not in human-readable form, as we cannot understand what is time by looking at this number. So, to make it human-readable form we'll follow the next method.

2) Using new Date() Method

You can simply initialize this object without passing any value. This will return the current date which is easy to understand.

Example 4:

var d = new Date();
console.log(d);

Output:

Sun May 15 2022 20:10:01 GMT+0530 (India Standard Time)

JavaScript Examples »



Related Examples



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.