如果 javascript "(new Date()).getTime()" 从 2 个不同的时区运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4577823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If javascript "(new Date()).getTime()" is run from 2 different Timezones
提问by user855
If JavaScript (new Date()).getTime()
is run from 2 different timezones simultaneously, will you get the same value?
如果 JavaScript(new Date()).getTime()
同时从 2 个不同的时区运行,你会得到相同的值吗?
Will this value be affected by the system time set on the machine where the browser is running?
该值是否会受到运行浏览器的机器上设置的系统时间的影响?
回答by Matthew Flaschen
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):
ECMAScript 标准说(§15.9.1.1):
"Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC."
“时间在 ECMAScript 中以 UTC 时间 1970 年 1 月 1 日以来的毫秒为单位进行测量。”
回答by PeanutPower
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:
我的 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
getTime 值之间的差异是 48,369 毫秒 (48s) 不同步,而不是 1 小时的区域差异
回答by darioo
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.
您不会获得相同的值 - 两个客户端浏览器获取系统时间的差异,但如果他们的时间设置正常,您应该获得两次,因为使用 获取时间戳new Date()
,您可以获得 UTC 值(new Date()
返回自 1970 年 1 月 1 日以来经过的毫秒数,并且不会改变),这是世界时并且与位置无关。
回答by BGerrissen
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.
机器之间获得的时间之间很可能总是存在偏差,但是(我之前错了)JavaScript Date() 将 UTC 时区作为默认值。
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.
通常,当时间很重要时,最好简单地使用服务器时间,并在需要时对输出中的时间应用时区校正。
回答by mplungjan
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));