#coding
#programming
#get timestamp
#get current timestamp
#webdevelopment

How to Get the Timestamp in JavaScript?

Anonymous

AnonymousJan 14, 2024

In this article, we will see How to get the timestamp in JavaScript.

Using Date.now() method

Date.now() is a built-in method in JavaScript that returns the current timestamp in milliseconds. This timestamp is calculated from the number of milliseconds that have elapsed since January 1, 1970, at 00:00:00 UTC, also known as the Unix Epoch.

let timestamp = Date.now();
console.log("Current Timestamp is:", timestamp)

Output

Current Timestamp is: 1705332755680

Using new Date().getTime() method

The Date() object represents dates and times. The getTime() method is a built-in method of the Date object that returns the numeric value corresponding to the time for the specified date according to universal time (UTC). The value is the number of milliseconds since January 1, 1970, 00:00:00 UTC, also known as the Unix Epoch.

let timestamp = new Date().getTime();
console.log("Current Timestamp is:", timestamp)

Output

Current Timestamp is: 1705332755680

Using new Date().valueOf() method

The valueOf() method is another way to obtain the numeric value of a Date object, similar to getTime(). When you call valueOf() on a Date object, it returns the primitive value of the object, which is the numeric representation of the date and time.

let timestamp = new Date().valueOf();
console.log("Current Timestamp is:", timestamp)

Output

Current Timestamp is: 1705332755680

Happy Coding  😎