简体   繁体   English

为什么Javascript中的getTime()与时区无关?

[英]Why getTime() in Javascript is timezone independent?

I was trying to understand if it's safe to execute (new Date()).getTime() in different timezones. 我试图了解在不同时区执行(new Date()).getTime()是否安全。

By reading this question, it seems that it actually is time zone independent (assuming that the time on the machine where the script is executed was set correctly). 通过阅读问题,似乎它实际上是与时区无关的(假设正确地设置了执行脚本的计算机上的时间)。

However, since it's the number of milliseconds since 1/1/1970, if we execute the same script from the next time zone, why it doesn't differ by 3600*1000 (the number of milliseconds in one hour)? 但是,由于它是自1970年1月1日以来的毫秒数,因此,如果我们从下一个时区执行相同的脚本,为什么它相差3600 * 1000(一小时内的毫秒数)不一样?

You're referring to UTC. 您指的是UTC。 It is always based on GMT time, so no matter where you execute the code from, GMT time is consistent. 它始终基于GMT时间,因此无论您从何处执行代码,GMT时间都是一致的。

Because it uses UTC 因为它使用UTC
From MDN MDN

getTime() always uses UTC for time representation. getTime()始终使用UTC进行时间表示。 For example, a client browser in one timezone, getTime() will be the same as a client browser in any other timezone. 例如,一个时区中的客户端浏览器getTime()将与其他时区中的客户端浏览器相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM