Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 694
syntaxerror Avatar asked Jun 01 '12 21:06

syntaxerror


People also ask

How can I get only the date from UTC?

JavaScript Date getUTCDate() getUTCDate() returns the day of the month (1 to 31) of a date object. getUTCDate() returns the day according to UTC.

How do you convert UTC time to milliseconds?

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); format. setTimeZone(TimeZone. getTimeZone("UTC")); Date date = format. parse(text); long millis = date.

Is new Date () getTime () UTC?

Use the getTime() method to get a UTC timestamp, e.g. new Date(). getTime() . The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation. Calling the method from any time zone returns the same UTC timestamp.


2 Answers

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

like image 92
advncd Avatar answered Oct 21 '22 17:10

advncd


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.)

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.)

like image 35
Pointy Avatar answered Oct 21 '22 18:10

Pointy