322

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.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Ben G
  • 26,091
  • 34
  • 103
  • 170
  • 3
    http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript – Narendra Yadala Nov 08 '11 at 08:20
  • 8
    Need a UTC timestamp specifically. The problem is that Date.getTime() alone depends on client timezone. – Ben G Nov 08 '11 at 08:29
  • 11
    No it doesn't. [Date.getTime](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getTime) returns a UTC timestamp. – wizzard Jan 30 '13 at 19:41
  • 5
    @wizzard could you explain why `new Date(2019, 3).getTime() !== Date.UTC(2019, 3)` then? Thanks. Also, if I create a new Date with getUTC... I will never have the correct start of year, month etc... – Romain Vincent Apr 18 '19 at 19:11
  • @RomainVincent you actually should test with TZ: `Date.UTC(2021, 15, 18, 4) == new Date(2021, 15, 18, 0).getTime() > true` - I'm now at 4 behind UTC. See https://stackoverflow.com/a/58884821/986862 – Andre Figueiredo May 18 '21 at 23:11

4 Answers4

261
new Date().getTime();

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

Community
  • 1
  • 1
ExpExc
  • 3,828
  • 1
  • 15
  • 20
  • 6
    Date.getTime() already returns the UTC timestamp. There is no need to apply an offset. See @james-mcmahon's answer below. – wizzard Feb 28 '13 at 22:53
  • 156
    For me in my angular app it returns my local time, not UTC. – waterplea Sep 18 '15 at 21:09
  • 30
    As a note, `getTime()` returns a Unix time-stamp adjusted for my local time instead of UTC. ie. `new Date(2016, 0, 1, 0, 0, 0).getTime()` returns 1451635200000 which is `8:00 AM Jan 1st 2016`. – Douglas Gaskell Jul 06 '16 at 18:09
  • 17
    @DouglasGaskell no, you are blaming the wrong guy here. `getTime()` returns the correct UTC number for a given input, it was the `new Date()` that returned the time in your local timezone - the midnight in the GMT-0800 timezone happened at `8:00 AM` UTC – Aprillion Aug 11 '16 at 08:33
  • 29
    thanks for the clarifications Aprillion! Finally I understand why people keep saying `new Date().getTime()` or `Date.now()` is UTC but when I try `console.log(new Date())`, it displays time in my timezone. The Date internally has the timestamp (milliseconds) in UTC (w/c is what is returned in `getTime()` or `now()`), and it also has "your timezone" information, so when the date is displayed, it automatically displays it in the correct (your) timezone. Try `new Date().toUTCString()` to see it in UTC. If you want, you can verify a return value of `Date.now()` here: https://currentmillis.com/ – Mon Aug 28 '18 at 03:12
  • 1
    @Mon You should put your comment as an answer. The existing answers, while "correct", won't work for many scenarios. – Josh Noe Sep 24 '18 at 17:54
  • 30
    This does not get the UTC date. I do not understand how this is accepted. – speciesUnknown Aug 27 '19 at 16:11
  • 3
    This is the correct answer! Why? Because "Jan 1, 1970, 00:00:00.000 GMT" is a singular immovable point in time from which the calculation is done as such it can not be more or less than the number of seconds elapsed since. as such it is in UTC, always. – Paul G Mihai Jun 30 '20 at 09:46
178

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.

Community
  • 1
  • 1
James McMahon
  • 48,506
  • 64
  • 207
  • 283
  • 31
    Date.now(); returns timestamp for my timezone, not UTC+0. So i have to modify it with new Date().getTimezoneOffset(); – Lukas Liesis Jan 30 '16 at 11:26
  • 12
    @LukasLiesis if you tested this by `console.log(new Date(timestamp))` then the timezone is a property of the Date object, not of the timestamp - the input must be in number of milliseconds since 1 January 1970 UTC in order for the Date object to display correct time in your local timezone... if you adjust the number, then the time will be incorrect – Aprillion Aug 11 '16 at 08:29
92

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

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/

Sarcastron
  • 1,527
  • 14
  • 20
Narendra Yadala
  • 9,554
  • 1
  • 28
  • 43
  • @ExpExc check closely, they are not the same. jsfiddle here http://jsfiddle.net/naryad/uU7FH/1/ – Narendra Yadala Nov 08 '11 at 09:15
  • 3
    For me this works only if I use getUTCMonth, getUTCDate, etc. At least when compared to `$ date -u +%s` – encoded Jan 30 '13 at 18:43
  • 7
    The UTC timestamp on the jsfiddle link is incorrect. Actually your "Local timestamp" is the UTC timestamp and your "UTC timestamp" is the # of seconds between epoch UTC and your local time, which isn't really meaningful. [Date.getTime](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getTime) automatically returns a UTC timestamp. – wizzard Jan 30 '13 at 19:37
  • @wizzard UTC timestamp is the # of seconds between epoch (1 January 1970 00:00:00 UTC) and UTC time. Local timestamp is the # of seconds between epoch and local time. `Date.getTime()` returns local timestamp where as `Date.UTC` returns the UTC timestamp. – Narendra Yadala Jan 31 '13 at 04:35
  • 6
    No, it doesn't. Read the [documentation](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getTime) I linked to. Look at your [jsfiddle](http://jsfiddle.net/naryad/uU7FH/1/) timestamps and compare them to the current UTC timestamp [here](http://www.unixtimestamp.com/) (or using `$ date -u +%s` as suggested above). Why don't you try parsing your timestamps back to dates and see what you come up with? – wizzard Jan 31 '13 at 04:44
  • 3
    Or heck, check out this question: http://stackoverflow.com/questions/9756120/utc-timestamp-in-javascript – wizzard Jan 31 '13 at 18:53
  • 2
    Yeah @wizzard is right, this method is not going to give you the correct UTC time. – James McMahon Feb 21 '13 at 20:48
  • @JamesMcMahon Well, the docs say so https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/UTC "The following statement creates a Date object using GMT instead of local time: `var gmtDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0));`" – Narendra Yadala Feb 22 '13 at 06:38
  • 1
    @Narendra Yadala, I think you are misunderstanding the docs, like wizzard said, compare the output of your method with the UTC timestamp at http://www.unixtimestamp.com/ – James McMahon Feb 22 '13 at 14:33
  • @JamesMcMahon @wizzard OP wants timestamp at the UTC timezone not the local timezone. `new Date().getTime()` returns timestamp (number of milliseconds between epoch and local time) relative to the local timezone. Where as the `Date.UTC` returns the timestamp relative to UTC timezone (number of milliseconds between epoch and UTC time). Hope that helps. Editing the answer to make it more clear. – Narendra Yadala Feb 23 '13 at 05:57
  • 1
    @NarendraYadala, Either I am completely missing something or your answer is still wrong. It doesn't match up the timestamp at unix time, which is showing the GMT (also know as UTC) timestamp. There is an explanation of what is going wrong here, http://stackoverflow.com/a/9756189/20774, "new Date(Y, M, D, h, m, s) treats its input as local time. If you pass in UTC time, of course you will get different results." – James McMahon Feb 23 '13 at 14:06
  • @JamesMcMahon My answer just assumes that the OP wants the unix time stamp at the UTC timezone and that is precisely what `Date.UTC` method does. `new Date().getTime()` gives unix time stamp at the local timezone. So what do I do, I create a current local date object using `new Date()` and then extract year, month, date, hrs, min, sec, ms from that object and pass it to `Date.UTC` method and that method returns the unix time stamp at the UTC timezone. – Narendra Yadala Feb 23 '13 at 17:19
  • 1
    I don't understand why you're so unwilling to simply compare the results of your code to the current UTC timestamp and see that it does not work. You can argue all you want but the fact is it is wrong. We have tried to explain why it is wrong but you make no effort to understand. Abandon all hope, ye who enter here. – wizzard Feb 27 '13 at 05:51
  • @wizzard I exactly know what I am doing here. Btw, what should I compare and with what? The output of `Date.UTC` is not the same as unix time stamp and I am well aware of this. Unix time stamp returns the local time stamp. OP wants the unix time stamp at the UTC timezone. They are obviously not the same values. Please read the comment of OP, "Need a UTC timestamp specifically. The problem is that Date.getTime() alone depends on client timezone. – babonk" – Narendra Yadala Feb 27 '13 at 06:02
  • [Unixtimestamp.com](http://www.unixtimestamp.com/) shows the current UTC time stamp as well as your local time stamp. We are asking you to compare your output to the UTC timestamp. If you were returning the UTC timestamp, the values would be similar, but they're not, BECAUSE YOUR CODE IS WRONG. – wizzard Feb 28 '13 at 16:53
  • @wizzard I have two timestamps here for january 12 10:30pm PST 1610519400 UTC 1610490600 If you plug both of those into unix timestamp converter you will be able to see what OP wants. The first time stamp is January 12 10:30pm PST from EPOCH The second timestamp is January 12 10:30PM UTC from EPOCH – Omar Dec 13 '20 at 07:47
  • This approach for getting UTC time is very helpful to get correct UTC date without time: `var now = new Date; Date.UTC(now.getUTCFullYear(),now.getUTCMonth(), now.getUTCDate())` – Lev Lukomsky Mar 30 '21 at 11:21
11

"... that are independent of their timezone"

var timezone =  d.getTimezoneOffset() // difference in minutes from GMT
abuduba
  • 4,986
  • 7
  • 26
  • 43