Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If javascript "(new Date()).getTime()" is run from 2 different Timezones

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?

like image 909
user855 Avatar asked Jan 02 '11 09:01

user855


People also ask

What timezone does new date () use?

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.

What is new date () getTime ()?

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

Does date now () depend on timezone?

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.

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

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

like image 84
Matthew Flaschen Avatar answered Sep 19 '22 16:09

Matthew Flaschen


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

like image 25
acheo Avatar answered Sep 22 '22 16:09

acheo