The Date.now() method is a static method in JavaScript, which means it’s called on the Date object itself, not an instance of the Date object. This method returns the current timestamp in milliseconds since the Unix Epoch (January 1, 1970, 00:00:00 UTC). The Unix Epoch serves as a reference point from which time is measured.

The returned value by Date.now() is a Number representing the milliseconds elapsed since the Unix Epoch.

Here’s how you can use it:

var timestamp = Date.now(); 
console.log(timestamp); // Outputs something like 1645299984000

This output represents the number of milliseconds passed since January 1, 1970, up until the exact moment Date.now() was called.

Using new Date().getTime()

The new Date().getTime() method in JavaScript is used to get the current timestamp. Unlike Date.now(), this method is called on an instance of a Date object.

The getTime() method returns the number of milliseconds since the Unix Epoch (January 1, 1970 00:00:00 UTC). The Unix Epoch is the time at which all time starts, and is a common reference point in computing.

Here’s how you can use new Date().getTime():

var timestamp = new Date().getTime();
console.log(timestamp); // Outputs something like 1645299984000

This output represents the number of milliseconds passed since January 1, 1970, up until the exact moment new Date().getTime() was called.

Using new Date().valueOf()

The new Date().valueOf() method in JavaScript is another way to get the current timestamp. This method is called on an instance of a Date object, similar to the new Date().getTime() method.

The valueOf() method returns the primitive value of a specified object. In the case of a Date object, it returns the number of milliseconds since the Unix Epoch (January 1, 1970, 00:00:00 UTC). The Unix Epoch is a point in time used as a reference point from which time is measured.

Here’s how you can use new Date().valueOf():

var timestamp = new Date().valueOf();
console.log(timestamp); // Outputs something like 1645299984000

This output represents the number of milliseconds passed since January 1, 1970, up until the exact moment new Date().valueOf() was called.

Using Date.parse(‘dateString’)

The Date.parse() method in JavaScript is used to parse a string representation of a date and convert it into a timestamp. It returns the number of milliseconds since January 1, 1970, 00:00:00 UTC, or NaN if the string is unrecognized or in an incorrect format.

Here’s how you can use Date.parse():

var timestamp = Date.parse('2024-02-19T19:13:07Z');
console.log(timestamp); // Outputs something like 1706045587000

In this example, ‘2024-02-19T19:13:07Z’ is a string representation of a date (in ISO 8601 format). The Date.parse() method converts this date string into a timestamp.

Using + operator with new Date()

The unary plus operator can also be used to convert a Date object into a timestamp. This operator precedes its operand and evaluates to its operand but attempts to convert it into a number.

Here’s how you can use the unary plus operator with a Date object:

var timestamp = +new Date();
console.log(timestamp); // Outputs something like 1645299984000

In this case, the unary plus operator is used to convert the current date and time (represented by new Date()) into a timestamp.

Convert Timestamp to seconds (Unix Timestamp)

It’s worth noting that all these methods will give you a timestamp in milliseconds. If you need a Unix timestamp (i.e., seconds since 1970/01/01), simply divide the result by 1000:

var timestampInSeconds = Math.floor(Date.now() / 1000);
console.log(timestampInSeconds); // Outputs something like 1645299984

In this example, timestampInSeconds holds the number of seconds that have passed since the Unix Epoch. This can be useful if you need to work with systems or APIs that use Unix timestamps.

Similar Posts