If javascript “(new Date()).getTime()” is run from

2019-01-17 19:31发布

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?

5条回答
在下西门庆
2楼-- · 2019-01-17 19:40

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

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-17 19:49

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

查看更多
贼婆χ
4楼-- · 2019-01-17 19:54

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.

查看更多
老娘就宠你
5楼-- · 2019-01-17 19:56

You might want to do this if you want the same date as your server across different timezones:

var UTC=new Date(Date.UTC(serverYear,serverMonth-1,serverDate,0,0,0,0)); 
查看更多
劫难
6楼-- · 2019-01-17 20:00

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.

查看更多
登录 后发表回答