我看到一些我不了解Javascript日期对象和DST过渡的行为。如果我在Chrome的javascript控制台中执行以下操作
var date = new Date(1268535600000); //2010-03-14T03:00:00.000Z (21:00 03-13 America/Chicago)
for(var i = 1; i <= 12; i++)
{
var time = date.getHours();
console.log(time)
console.log(date)
date.setHours(date.getHours() + 1);
}
输出是:
21
Sat Mar 13 2010 21:00:00 GMT-0600 (Central Standard Time)
22
Sat Mar 13 2010 22:00:00 GMT-0600 (Central Standard Time)
23
Sat Mar 13 2010 23:00:00 GMT-0600 (Central Standard Time)
0
Sun Mar 14 2010 00:00:00 GMT-0600 (Central Standard Time)
1
Sun Mar 14 2010 01:00:00 GMT-0600 (Central Standard Time)
1
Sun Mar 14 2010 01:00:00 GMT-0600 (Central Standard Time)
1
Sun Mar 14 2010 01:00:00 GMT-0600 (Central Standard Time)
1
Sun Mar 14 2010 01:00:00 GMT-0600 (Central Standard Time)
1
Sun Mar 14 2010 01:00:00 GMT-0600 (Central Standard Time)
1
Sun Mar 14 2010 01:00:00 GMT-0600 (Central Standard Time)
1
Sun Mar 14 2010 01:00:00 GMT-0600 (Central Standard Time)
1
Sun Mar 14 2010 01:00:00 GMT-0600 (Central Standard Time)
但是,将for循环的最后一行更改为
date = new Date(date.getTime() + 3600000)
产生我期望的转换时跳过小时的输出:
21
Sat Mar 13 2010 21:00:00 GMT-0600 (Central Standard Time)
22
Sat Mar 13 2010 22:00:00 GMT-0600 (Central Standard Time)
23
Sat Mar 13 2010 23:00:00 GMT-0600 (Central Standard Time)
0
Sun Mar 14 2010 00:00:00 GMT-0600 (Central Standard Time)
1
Sun Mar 14 2010 01:00:00 GMT-0600 (Central Standard Time)
3
Sun Mar 14 2010 03:00:00 GMT-0500 (Central Daylight Time)
4
Sun Mar 14 2010 04:00:00 GMT-0500 (Central Daylight Time)
5
Sun Mar 14 2010 05:00:00 GMT-0500 (Central Daylight Time)
6
Sun Mar 14 2010 06:00:00 GMT-0500 (Central Daylight Time)
7
Sun Mar 14 2010 07:00:00 GMT-0500 (Central Daylight Time)
8
Sun Mar 14 2010 08:00:00 GMT-0500 (Central Daylight Time)
9
Sun Mar 14 2010 09:00:00 GMT-0500 (Central Daylight Time)
第一种方法不起作用的原因是什么?
编辑: 此外,通过重复小时的DST转换,似乎只是忽略第一种方法的重复小时: 另一件事是,如果我尝试使用重复小时的DST转换,它似乎忽略了重复的小时:
Sun Nov 07 2010 00:00:00 GMT-0500 (Central Daylight Time)
Sun Nov 07 2010 01:00:00 GMT-0600 (Central Standard Time)
Sun Nov 07 2010 02:00:00 GMT-0600 (Central Standard Time)
但它使用第二种方法正确处理重复小时。
答案 0 :(得分:1)
也许这是一个错误。你有没有在多个浏览器上试过它?
否则我猜是因为你试图将小时数设置为不存在的时间,所以它拒绝改变。