Organizational Research By

Surprising Reserch Topic

How do you get a timestamp in JavaScript?

How can I get a timestamp in JavaScript?

Something similar to Unix's timestamp, that is, a single number that represents the current time and date. Either as a number or a string.

asked Jul 17, 2015 in JAVASCRIPT by rajesh
0 votes

Related Hot Questions

1 Answer

0 votes
On almost all current browsers you can use 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 (! { = function() { return new Date().getTime(); }
To get the timestamp in seconds, you can use:

Math.floor( / 1000)
Or alternatively you could use: / 1000 | 0
Which should be slightly faster, but also less readable (also see this answer).

I would recommend using (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 miliseconds:

new Date().getTime()
Which you can then convert to seconds using the same method as above.
answered Jul 17, 2015 by rajesh