How do you get a timestamp in JavaScript?

Asked : Nov 17

Viewed : 20 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).

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

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
1524379940

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

Date.now()

You could get the same value by calling

new Date().getTime()

or

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

~~(Date.now() / 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 Date.now() 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 = Date.now();
console.log(timestamp);
    
// Converting it back to human-readable date and time
var d = new Date(timestamp);
console.log(d);

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

Syntax

Date.getTime()

Parameters

NONE

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.