Javascript日期对象和夏令时间 [英] Javascript Date objects and Daylight Savings Time

查看:138
本文介绍了Javascript日期对象和夏令时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些行为,我不明白与Javascript日期对象和DST转换。如果我在Chrome的JavaScript控制台执行以下操作

I'm seeing some behavior I don't understand with Javascript date objects and DST transitions. If I execute the following in Chrome's javascript console

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)

,改变最后一行操作到

However, changing the last line of the for loop to

date = new Date(date.getTime() + 3600000)   

产生我期望跳过的小时在过渡期间的输出:

produces the output I'd expect with the skipped hour at the transition:

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转换,它似乎忽略了重复的小时:

Additionally, with a DST transition with a duplicated hour it seems to just ignore the duplicated hour with the first approach: The other thing is that if I try it with a DST transition with a duplicated hour it just seems to ignore the duplicated hour:

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)

但它正确处理重复的小时第二种方法。

But it correctly handles the duplicated hour with the second approach.

推荐答案

也许这是一个错误。您是否在多个浏览器上尝试过?

Maybe it's a bug. Have you tried it on more than one browser?

否则我猜想,由于您尝试将时间设置为不存在的时间拒绝变更。

Otherwise I'd guess that since you're trying to set the hours to a time that doesn't exist, it rejects the change.

这篇关于Javascript日期对象和夏令时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆