JavaScript的时区偏移因日期的年份不同而不同。

huangapple go评论66阅读模式
英文:

Javascript timezone-offset different depending on the year of the date

问题

在JavaScript中实例化日期时,时区偏移因年份不同而不同。我住在比利时,所以UTC时区偏移是+1。

从1943年开始,时区似乎与预期相同,但在此之前,它根据年份不同会产生不同的结果。

例如(您可以在jsfiddle.net上尝试):

// 所有日期都是1月1日,只有年份不同

console.log(new Date(1900, 0, 1).toString())
// Mon Jan 01 1900 00:00:00 GMT+0000(中欧标准时间)

console.log(new Date(1940, 0, 1).toString())
// Mon Jan 01 1940 00:00:00 GMT+0000(中欧标准时间)

console.log(new Date(1941, 0, 1).toString())
// Wed Jan 01 1941 00:00:00 GMT+0200(中欧标准时间)

console.log(new Date(1943, 0, 1).toString())
// Fri Jan 01 1943 00:00:00 GMT+0100(中欧标准时间)

console.log(new Date(2013, 0, 1).toString())
// Tue Jan 01 2013 00:00:00 GMT+0100(中欧标准时间)

有人能解释为什么时区偏移因年份不同而不同吗?

英文:

When instantiating a date in Javascript the timezone-offset differs depending on the year.
I live in Belgium so the utc timezone offset is +1.

Starting from year 1943 the timezone seems to be as expected but before it gives different result depending on the year.

For example (you can try it out in jsfiddle.net):

// All dates are 1 january, only the year differs

console.log(new Date(1900,0,1).toString())
// Mon Jan 01 1900 00:00:00 GMT+0000 (Central European Standard Time)

console.log(new Date(1940,0,1).toString())
// Mon Jan 01 1940 00:00:00 GMT+0000 (Central European Standard Time)

console.log(new Date(1941,0,1).toString())
// Wed Jan 01 1941 00:00:00 GMT+0200 (Central European Standard Time)

console.log(new Date(1943,0,1).toString())
// Fri Jan 01 1943 00:00:00 GMT+0100 (Central European Standard Time)

console.log(new Date(2013,0,1).toString())
// Tue Jan 01 2013 00:00:00 GMT+0100 (Central European Standard Time)

Can someone explain why the timezone offset differs depending on the year?

答案1

得分: 2

这是因为夏令时偏移和时区偏移的更改。一个国家的政府可能会定期更改它们。

这就是你不应该手动计算时间秒数而应该使用JS日期函数的原因。JS对特定位置/时区标签的夏令时历史和时区更改有充分的了解。

https://en.wikipedia.org/wiki/Daylight_saving_time

英文:

That's because of the Daylight saving time offset and time zone offset changes. A country's government could change them periodically.

That's the reason you shouldn't do your datetime math manually on seconds but use JS Date functions. JS has full knowledge of the history of DST and time zone changes for a particular location / time zone label.

https://en.wikipedia.org/wiki/Daylight_saving_time

huangapple
  • 本文由 发表于 2023年6月12日 16:04:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定