Getting current date in milliseconds (UTC) (NO use of strings)

Well, you might think that this question has already been asked, but I think it has not. The solutions I’ve read about all had this “jigsaw puzzle” technique (like getUTCMonth() + getUTCMinutes + ...).
But as I only want to compare the elapsed seconds between two UTC (!) dates, this does not apply.

As everybody knows, you can get the current (non-UTC) date by:

var d = new Date();
var t_millis = d.getTime();

But this is NOT what I want. I’d like to have the current system date in UTC and in milliseconds, so not mess about with strings at all. AFAIK the variable t_millis will contain the millisecond value of the current timestamp in GMT, not UTC.
(Since d is in GMT as well. Unless getTime() does a sort of implicit time zone conversion, i. e. adding the offset BEFORE giving out the milliseconds, but I’ve never read about that anywhere)

So is there really no other way than adding the offset to the time value?
I’m desperately missing a function like getUTCTimeMillis() known from other languages.

This is an old question but for the sake of the new visitors here is THE CORRECT ANSWER:;

It returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC

The millisecond value of the time-of-day is going to be the same regardless of your time zone. That is, there are no time zones on planet Earth that differ from one another by a number of milliseconds greater than zero. (They may differ by an integer number of hours or even minutes, but not seconds or milliseconds.)

Read More:   Can I extend Proxy with an ES2015 class?

That said, the value you get back from getTime() is a UTC-relative timestamp. If two web browsers at widely different spots on the globe create a Date object at the same time, they’ll both get the same value from .getTime() (assuming the clocks are synchronized, which is of course highly unlikely).

Here: 1338585185539 That’s a timestamp I just got from my browser. I’m in Austin, TX, and now it’s 4:13 in the afternoon (so that timestamp will be from slightly before that). Plug it into a Date instance on your machine and see what it says.

(edit — for posterity’s sake, that timestamp is from 1 June 2012.)

how about:

var now = new Date();
var utc_now = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
console.log('UTC: ' + utc_now) // correct UTC time but wrong timezone!
console.log('UTC (in ms): ' + utc_now.getTime())

I have used this function to solve the problem.

function getUTCNow()
    var now = new Date();
    var time = now.getTime();
    var offset = now.getTimezoneOffset();
    offset = offset * 60000;
    return time - offset;
  • The getTime function returns the number of milliseconds elapsed since
    1 January 1970 00:00:00 in the client timezone.
  • getTimezoneOffset return offset in minutes between Client timezone and UTC.
  • offset = offset * 60000; this operation transform minutes in miliseconds.
  • subtracting the offset get the number of milliseconds elapsed since 1
    January 1970 00:00:00 UTC.

To get the timestamp from a date in UTC, you need to take in consideration the timezone and daylight savings for that date. For example, a date in January or in July could mean 1 hour difference.

Read More:   Navigation is blocked error in Chrome 61+ on Android

The option below does just that.

Date.prototype.getUTCTime = function () {
    return this.getTime() - (this.getTimezoneOffset() * 60000);

It can be used as in:

var date = new Date();
var timestamp = date.getUTCTime();

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts