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

syntaxerror picture syntaxerror · Jun 1, 2012 · Viewed 57.4k times · Source

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.

Answer

advncd picture advncd · Dec 6, 2015

This is an old question but for the sake of the new visitors here is the CORRECT answer:

Date.now();

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