How to get time in milliseconds since the unix epoch in Javascript?

Javascript

Javascript Problem Overview


> Possible Duplicate:
> How do you get a timestamp in JavaScript?
> Calculating milliseconds from epoch

How can I get the current epoch time in Javascript? Basically the number of milliseconds since midnight, 1970-01-01.

Javascript Solutions


Solution 1 - Javascript

Date.now() returns a unix timestamp in milliseconds.

const now = Date.now(); // Unix timestamp in milliseconds
console.log( now );

Prior to ECMAScript5 (I.E. Internet Explorer 8 and older) you needed to construct a Date object, from which there are several ways to get a unix timestamp in milliseconds:

console.log( +new Date );
console.log( (new Date).getTime() );
console.log( (new Date).valueOf() );

Solution 2 - Javascript

This will do the trick :-

new Date().valueOf() 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionantonpugView Question on Stackoverflow
Solution 1 - JavascriptPaulView Answer on Stackoverflow
Solution 2 - Javascriptraina77owView Answer on Stackoverflow