How do you get a timestamp in JavaScript?

Asked : Nov 17

Viewed : 34 times

How do you get a date in JavaScript?

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

datetime javascript timestamp date-arithmetic 

Nov 17

4 Answers

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 explanation 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 the 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 :;


answered Jan 10

The UNIX timestamp is an integer that represents the number of seconds elapsed since January 1, 1970.

On UNIX-like machines, which include Linux and macOS, you can type date +%s in the terminal and get the UNIX timestamp back:

$ date +%s

The current timestamp can be fetched by calling the now() method on the Date object:

You could get the same value by calling

new Date().getTime()


new Date().valueOf()

Note: IE8 and below do not have the now() method on Date. Look for a polyfill if you need to support IE8 and below, or use new Date().getTime() if is undefined (as that’s what a polyfill would do)

The timestamp in JavaScript is expressed in milliseconds.

To get the timestamp expressed in seconds, convert it using:

Math.floor( / 1000)

Note: some tutorials use Math.round(), but that will approximate to the next second even if the second is not fully completed.

or, less readable:

~~( / 1000)

I’ve seen tutorials using

+new Date

which might seem a weird statement, but it’s perfectly correct JavaScript code. The unary operator + automatically calls the valueOf() method on any object it is assigned to, which returns the timestamp (in milliseconds). The problem with this code is that you instantiate a new Date object that’s immediately discarded.

To generate a date from a timestamp, use new Date(<timestamp>) but make sure you pass a number (a string will get you an “invalid date” result - use parseInt() in doubt)

answered Jan 10

You can simply use the JavaScript method to generate the UTC timestamp in milliseconds (which is the number of milliseconds since midnight Jan 1, 1970).

The following example demonstrates how to get a timestamp and how to convert it back to the human-readable date and time format in JavaScript.

// Creating a timestamp
var timestamp =;
// Converting it back to human-readable date and time
var d = new Date(timestamp);

The method is supported in all major web browsers. See the tutorial on JavaScript date and time to learn more about date and time formatting in JavaScript.

answered Jan 10

getTime() returns the number of milliseconds since January 1, 1970 00:00:00.





Return Value

A number : Number of milliseconds since January 1, 1970 00:00:00.

answered Jan 10

Login and Submit Your Answer
Browse other questions tagged  datetime  javascript  timestamp  date-arithmetic or ask your own question.