Coder Perfect

[duplicate] Get a UTC timestamp


In JavaScript, how can I get the current UTC timestamp? I’d like to accomplish this so that I can send client-side timestamps that are independent of their timezone.

Asked by bgcode

Solution #1

new Date().getTime();

See @James McMahon’s response for further details.

Answered by ExpExc

Solution #2

The correct way is, as Wizzard pointed out.

new Date().getTime();

or, if you’re using Javascript 1.5, just type;

From the documentation,

You can use if you want to make a time stamp without milliseconds.

Math.floor( / 1000);

I wanted to make this a response so that the correct way would be more obvious.

At, you can compare ExpExc’s and Narendra Yadala’s results to the approach above, and verify with or by running date + percent s on a Unix terminal.

Answered by James McMahon

Solution #3

To acquire the time stamp in the UTC timezone, use the Date.UTC method.


var now = new Date;
var utc_timestamp = Date.UTC(now.getUTCFullYear(),now.getUTCMonth(), now.getUTCDate() , 
      now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());

Live demo here

Answered by Narendra Yadala

Solution #4

“… that don’t care about their time zone”

var timezone =  d.getTimezoneOffset() // difference in minutes from GMT

Answered by abuduba

Post is based on