If JavaScript (new Date()).getTime()
is run from 2 different timezones simultaneously, will you get the same value?
Will this value be affected by the system time set on the machine where the browser is running?
The date object itself (as shown by your link) is the number of milliseconds since 1 January 1970 UTC - so therefore the Date itself doesn't have a timezone - it's just a number. It's only when you display it with toString that you see YOUR timezone.
The date. getTime() method is used to return the number of milliseconds since 1 January 1970. when a new Date object is created it stores the date and time data when it is created. When the getTime() method is called on this date object it returns the number of milliseconds since 1 January 1970 (Unix Epoch).
Yes, Date. now() will give you the same UTC timestamp independent of your current timezone. Such a timestamp, rather a point in time, does not depend on timezones. The Java equivalent new Date() gives you the exact same thing.
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.
Yes, it's affected by system time. However, if the local time is correct (for whatever time zone the computer's set to), it should be the same in any time zone.
The ECMAScript standard says (§15.9.1.1):
"Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC."
Code:
var today = new Date(); console.log(today); var t = today.getTime(); console.log(t);
My Computer in the UK:
Sat Sep 21 2013 03:45:20 GMT+0100 (GMT Daylight Time) 1379731520112
My VPS:
Sat, 21 Sep 2013 02:44:31 GMT 1379731471743
Difference between getTime values is 48,369 milliseconds (48s) out of sync not the 1 hour zone difference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With