需要使用Javascript Date Object显示DST转换的本地时间 [英] Need to display local times over DST transitions using Javascript Date Object

查看:164
本文介绍了需要使用Javascript Date Object显示DST转换的本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JavaScript(在Firefox等网络浏览器中)的时间(小时)内输出一系列时间。这一系列的时间将在短暂的一天(在春天失去一个小时)和漫长的一天(秋天获得一个小时)重叠。我正在寻找的输出是在当地时间,即应用时区和DST偏移。例如,在英国,我们在短时间内从01:00到01:59失踪一小时,输出将是:



00:00, 02:00,03:00



在漫长的一天中,我们从01:00到02:00有一个额外的时间,输出将是:



00:00,01:00,01:00,02:00,03:00



我已经找到这两个辉煌的答案突出了一些陷阱,并解决了我的一些问题:





但是真正的困难在于使javascript意识到上述第二个问题中确定的这个缺失和额外的小时(可以这么说)。



<我认为这样做的一个潜在解决方案是在UTC(也称为GMT)运行,只需要转换到当地时间,但我很难看到我能做到这一点。



有没有人有任何关于如何获得我以后的想法?

解决方案

如果你有一个固定的时区,以下JavaScript代码似乎工作(在最后一个chrome版本和firefox 6上进行测试):

  //设置日期到11/04/2012 at 00:00 UTC 
var date = new Date(1331424000000);
for(var i = 1; i< = 12; i ++){
$('。data-dston')。append(''+ date.getHours()+':00, );
date = new Date(date.getTime()+ 3600000)
}

//将日期设置为04/11/2012 at 00:00 UTC
var date = new Date(1351987200000);
for(var i = 1; i< = 12; i ++){
$('。data-dstoff')。append(''+ date.getHours()+':00, );
date = new Date(date.getTime()+ 3600000)
}

这是一个JSFiddle: http://jsfiddle.net/Vsd2A/3/ 查看代码动作!


I am trying to output a series of times in hour (on the hour) intervals within Javascript (so inside a web browser such as Firefox). This series of times will overlap the short day (losing an hour in spring) and long day (gaining an hour in autumn). The output I'm looking for is in local time, i.e. with timezone and DST offsets applied. So for example, in the UK we have a missing hour from 01:00 to 01:59 on the short day such that the output would be:

00:00, 02:00, 03:00

And on the long day we have an extra hour from 01:00 to 02:00 such that the output would be:

00:00, 01:00, 01:00, 02:00, 03:00

I have already found these two brilliant answers that highlight some pitfalls and address part of my problem:

But the real difficulty is in making javascript aware of this missing and extra hour (so to speak) as identified in the second question mentioned above.

I think a potential solution to this would be to operate in UTC (aka GMT) and just do a conversion to local time but I'm struggling to see how I could do this.

Does anyone have any ideas about how to achive what I'm after?

解决方案

If you have a fixed timezone, the following javascript code seems to work (tested on the last chrome version and firefox 6) :

 // set the date to 11 / 04 / 2012 at 00:00 UTC
 var date = new Date(1331424000000);
 for(var i = 1; i <= 12; i++) {   
     $('.data-dston').append(' ' + date.getHours() + ':00, ');
     date = new Date(date.getTime() + 3600000)
 }

 // set the date to 04 / 11 / 2012 at 00:00 UTC
 var date = new Date(1351987200000);
 for(var i = 1; i <= 12; i++) {   
     $('.data-dstoff').append(' ' + date.getHours() + ':00, ');
     date = new Date(date.getTime() + 3600000)
 }

Here's a JSFiddle : http://jsfiddle.net/Vsd2A/3/ to see the code in action !

这篇关于需要使用Javascript Date Object显示DST转换的本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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