datetime - How do you get a timestamp in JavaScript?

ID : 77

viewed : 233

Tags : javascriptdatetimetimestampdate-arithmeticjavascript

Top 5 Answer for datetime - How do you get a timestamp in JavaScript?

vote vote

99

Short & Snazzy:

+ new Date() 

A unary operator like plus triggers the valueOf method in the Date object and it returns the timestamp (without any alteration).

Details:

On almost all current browsers you can use Date.now() to get the UTC timestamp in milliseconds; a notable exception to this is IE8 and earlier (see compatibility table).

You can easily make a shim for this, though:

if (!Date.now) {     Date.now = function() { return new Date().getTime(); } } 

To get the timestamp in seconds, you can use:

Math.floor(Date.now() / 1000) 

Or alternatively you could use:

Date.now() / 1000 | 0 

Which should be slightly faster, but also less readable.
(also see this answer or this with further explaination to bitwise operators).

I would recommend using Date.now() (with compatibility shim). It's slightly better because it's shorter & doesn't create a new Date object. However, if you don't want a shim & maximum compatibility, you could use the "old" method to get the timestamp in milliseconds:

new Date().getTime() 

Which you can then convert to seconds like this:

Math.round(new Date().getTime()/1000) 

And you can also use the valueOf method which we showed above:

new Date().valueOf() 

Timestamp in Milliseconds

var timeStampInMs = window.performance && window.performance.now && window.performance.timing && window.performance.timing.navigationStart ? window.performance.now() + window.performance.timing.navigationStart : Date.now();  console.log(timeStampInMs, Date.now());

vote vote

87

I like this, because it is small:

+new Date 

I also like this, because it is just as short and is compatible with modern browsers, and over 500 people voted that it is better:

Date.now() 
vote vote

75

JavaScript works with the number of milliseconds since the epoch whereas most other languages work with the seconds. You could work with milliseconds but as soon as you pass a value to say PHP, the PHP native functions will probably fail. So to be sure I always use the seconds, not milliseconds.

This will give you a Unix timestamp (in seconds):

var unix = Math.round(+new Date()/1000); 

This will give you the milliseconds since the epoch (not Unix timestamp):

var milliseconds = new Date().getTime(); 
vote vote

65

var time = Date.now || function() {   return +new Date; };  time(); 
vote vote

58

I provide multiple solutions with descriptions in this answer. Feel free to ask questions if anything is unclear


Quick and dirty solution:

Date.now() /1000 |0 

Warning: it might break in 2038 and return negative numbers if you do the |0 magic. Use Math.floor() instead by that time

Math.floor() solution:

Math.floor(Date.now() /1000); 

Some nerdy alternative by Derek 朕會功夫 taken from the comments below this answer:

new Date/1e3|0 

Polyfill to get Date.now() working:

To get it working in IE you could do this (Polyfill from MDN):

if (!Date.now) {     Date.now = function now() {         return new Date().getTime();     }; } 

If you do not care about the year / day of week / daylight saving time you need to remember this for dates after 2038:

Bitwise operations will cause usage of 32 Bit Integers instead of 64 Bit Floating Point.

You will need to properly use it as:

Math.floor(Date.now() / 1000) 

If you just want to know the relative time from the point of when the code was run through first you could use something like this:

const relativeTime = (() => {     const start = Date.now();     return () => Date.now() - start; })(); 

In case you are using jQuery you could use $.now() as described in jQuery's Docs which makes the polyfill obsolete since $.now() internally does the same thing: (new Date).getTime()

If you are just happy about jQuery's version, consider upvoting this answer since I did not find it myself.


Now a tiny explaination of what |0 does:

By providing |, you tell the interpreter to do a binary OR operation.
Bit operations require absolute numbers which turns the decimal result from Date.now() / 1000 into an integer.

During that conversion, decimals are removed, resulting in a similar result to what using Math.floor() would output.

Be warned though: it will convert a 64 bit double to a 32 bit integer.
This will result in information loss when dealing with huge numbers.
Timestamps will break after 2038 due to 32 bit integer overflow unless Javascript moves to 64 Bit Integers in Strict Mode.


For further information about Date.now follow this link: Date.now() @ MDN

Top 3 video Explaining datetime - How do you get a timestamp in JavaScript?

Related QUESTION?