How do I get a timestamp in JavaScript?

Timestamp in milliseconds

To get the number of milliseconds since Unix epoch, call

Alternatively, use the unary operator + to call Date.prototype.valueOf:

+ new Date()

Alternatively, call valueOf directly:

new Date().valueOf()

To support IE8 and earlier (see compatibility table), create a shim for

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

Alternatively, call getTime directly:

new Date().getTime()

Timestamp in seconds

To get the number of seconds since Unix epoch, i.e. Unix timestamp:

Math.floor( / 1000)

Alternatively, using bitwise-or to floor is slightly faster, but also less readable and may break in the future (see explanations 1, 2): / 1000 | 0

Timestamp in milliseconds (higher resolution)


var isPerformanceSupported = (
    window.performance && &&
    window.performance.timing &&

var timeStampInMs = (
    isPerformanceSupported ? +
    window.performance.timing.navigationStart :


Leave a Comment