Categories
date-arithmetic datetime javascript timestamp

How do you get a timestamp in JavaScript?

4560

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.

1

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

    – user16559547

    Aug 3, 2021 at 15:30

5661

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

3

616

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

11

  • 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 Date.now() 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

310

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

0