Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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.

like image 588
antonpug Avatar asked Mar 05 '12 23:03

antonpug


People also ask

How do you get milliseconds since epoch?

The Date. now() and new Date(). getTime() calls retrieve the milliseconds since the UTC epoch. Convert the milliseconds since epoch to seconds by dividing by 1000.

How do I get the current epoch time in JavaScript?

The getTime() method in the JavaScript returns the number of milliseconds since January 1, 1970, or epoch. If we divide these milliseconds by 1000 and then integer part will give us the number of seconds since epoch.

How do you write milliseconds in JavaScript?

JavaScript - Date getMilliseconds() Method Javascript date getMilliseconds() method returns the milliseconds in the specified date according to local time. The value returned by getMilliseconds() is a number between 0 and 999.


2 Answers

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() );
like image 136
Paul Avatar answered Oct 15 '22 01:10

Paul


This will do the trick :-

new Date().valueOf()  
like image 31
raina77ow Avatar answered Oct 15 '22 00:10

raina77ow