user855
user855

Reputation: 19918

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?

Upvotes: 48

Views: 31528

Answers (4)

acheo
acheo

Reputation: 3126

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

Upvotes: 28

Matthew Flaschen
Matthew Flaschen

Reputation: 284796

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

Upvotes: 52

BGerrissen
BGerrissen

Reputation: 21680

There will most likely always be a deviation between times attained between machines, but (I was wrong before) JavaScript Date() takes the UTC timezone as default.

Usually when time is essential, it's best to simply use the Server time and apply timezone corrections to that in the output if required.

Upvotes: 2

darioo
darioo

Reputation: 47183

You won't get the same value - difference between two client's browsers picking up their system time, but if their time is set up ok, you should get two times with a minimal difference since getting the timestamp using new Date(), you can get the UTC value (new Date() returns number of milliseconds ellapsed since January 1, 1970, and that won't change), which is universal time and is location agnostic.

Upvotes: 8

Related Questions