date-arithmetic datetime javascript timestamp

How do you 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.


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

    – user16559547

    Aug 3, 2021 at 15:30


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).


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 or this with further explaination to bitwise operators).

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 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.timing && window.performance.timing.navigationStart ? + window.performance.timing.navigationStart :;




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:


  • 12

    @Billy As I recall it, I computed the timestamp in the two suggested solutions 1M times each, and calculated the average runtime. I ran it in Firefox and Chrome, with getTime being faster in both browsers. That said, even if it were (marginally) slower I’d choose new Date().getTime(). Luckily for me, the faster solution is already the legible solution!

    Jul 8, 2013 at 12:44

  • 11

    Agreed with @FabrícioMatté. Unary operator behavior may not be rudimentary, but if you haven’t brushed up on it, don’t expect to be able to function effectively in a lot of teams.

    Jul 18, 2013 at 1:58

  • 10

    @Niklaus That’s because you’re concatenating it to another string. In that case, new Date().toString() is called.

    – kirb

    Oct 2, 2013 at 11:50

  • 3

    + is unary mathmatic in this case. typeof (+new Date()) evaluates to “number”. Basically it’s shorthand for new Date().valueOf() — without the javascript function call. But don’t write it this way, it looks like a typo.

    – ansiart

    Dec 20, 2014 at 22:58

  • 3

    @FelixGagnon-Grenier I added because it is now the preferred way, and lots of people don’t like +new Date. I like that daveb’s answer now includes +new Date, and explains it, but it didn’t used to either.

    – xer0x

    Jan 4, 2017 at 0:15


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();