Alex Rivera | Logout

Get a UTC timestamp

Asked 2011-11-08T08:18:22.300
341

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.

Edit
Report

2 Answers

267

const timestamp = new Date().getTime()

console.log(timestamp)

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

answered 2011-11-08T08:49:17.710
182

As wizzard pointed out, the correct method is,

new Date().getTime();

or under Javascript 1.5, just

Date.now();

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(Date.now() / 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 http://jsfiddle.net/JamesFM/bxEJd/, and verify with http://www.unixtimestamp.com/ or by running date +%s on a Unix terminal.

answered 2013-02-21T21:02:59.640

Your Answer