Get a UTC timestamp [duplicate]

How can I get the current UTC timestamp in JavaScript? I want to do this so I can send timestamps from the client-side that are independent of their timezone.


new Date().getTime();

For more information, see @James McMahon's answer.


As wizzard pointed out, the correct method is,

new Date().getTime();

or under Javascript 1.5, just;

From the documentation,

The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC.

If you wanted to make a time stamp without milliseconds you can use,

Math.floor( / 1000);

I wanted to make this an answer so the correct method is more visible.

You can compare ExpExc's and Narendra Yadala's results to the method above at, and verify with or by running date +%s on a Unix terminal.


"... that are independent of their timezone"

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

You can use Date.UTC method to get the time stamp at the UTC timezone.


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


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.