Coder Perfect

[duplicate] Get a UTC timestamp

Problem

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

Date.now();

From the documentation,

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

Math.floor(Date.now() / 1000);

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

At http://jsfiddle.net/JamesFM/bxEJd/, you can compare ExpExc’s and Narendra Yadala’s results to the approach above, and verify with http://www.unixtimestamp.com/ 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.

Usage:

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 http://jsfiddle.net/naryad/uU7FH/1/

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 https://stackoverflow.com/questions/8047616/get-a-utc-timestamp