是使用getTimezoneOffset()夏令过渡时期稳定吗? [英] Is getTimezoneOffset() stable during daylight saving transition?

查看:285
本文介绍了是使用getTimezoneOffset()夏令过渡时期稳定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我有在Javascript是转换UTC时间成通过时区的本地时间转换逻辑日期时间。我想知道这个逻辑将日光转换过程中正常工作?如果没有什么是该补救办法?我不能使用任何第三方库。我必须使用纯JavaScript或角JS。

\r
\r

函数数值指明MyTime()\r
        {\r
        \r
        VAR D1 =的document.getElementById(txtDate)值。\r
        VAR zOffset =的document.getElementById(txtOffset)值。\r
        的console.log(日期1,D1);\r
        \r
        VAR D2 =新的日期(d1.replace(/ /克,'T'));\r
        \r
        变种D3 = d2.getTime()+(d2.getTimezoneOffset()* 60000);\r
        \r
        的console.log(日期2,D2);\r
        的console.log(DATE3,D3);\r
        VAR D4 =新的日期(D3 +(3600000 * zOffset));\r
        \r
        的console.log(DATE3,D3);\r
        的console.log(日期4,D4);\r
        \r
        变种D5 = d4.toLocaleTimeString();\r
        的console.log(日期5,D5);\r
        \r
        // D6 = d5.match(/(\\ d +)(=:\\ D +?:\\ D +)|([A-Z] +)(= $)/ G)。加入();\r
        \r
        //console.log(\"Date6\",d6)\r
        \r
        的document.getElementById(hourValue)的innerHTML = D5。\r
        }

\r

< H1>\r
时区\r
< / H1>\r
&所述H 2的id =hourValue>\r
\r
< / H>\r
<表>\r
&所述; TR>\r
  < TD>日期时间和LT; / TD>\r
  < TD><输入类型=文本ID =txtDate值=2016年4月10日09:00:00.0/>< / TD>\r
< / TR>\r
< TR>< TD>时区和LT; / TD>< / TR>\r
< TR>< TD><输入类型=文本ID =txtOffsetVALUE = - 5.00/>< / TD>< / TR>\r
< TR>< TD><输入类型=提交ID =btnSubmit按钮值=转换的onClick =数值指明MyTime()/>< / TD>< / TR>\r
< /表>

\r

\r
\r


解决方案

使用getTimezoneOffset 的只是获取从主机系统偏移,所以它是稳定作为主机。

注:与Date构造函数的字符串分析几乎完全依赖于实现和非常不可靠的,就是不去做。始终手动解析字符串。图书馆可以提供帮助,但对于一个特定格式的解析函数为2线(3验证)。

当你改变2016年4月10日09:00:00.0到2016-04-10T09:00:00.0您已经创建了一个ISO 8601的日期和时间字符串没有一个时区。如果正确解析(在使用浏览器的约10%将无法解析它在所有),它的的被视为当地的日期和时间(铬错误地将它作为UTC,IE 11正确当地),这样主机系统设置将被用于调整创建日期的UTC时间值,以便它重新presents时刻相同的时刻。

例如

 警报(新日期(2016-04-10T09:00:00.0));

应该打印的日期2016年4月10日,上午09:00无论主机系统的时区偏移的(但不是在把它当作UTC,这是错误的浏览器)。

如果你想找出一些其它时区的等效时间,使用UTC的方法来调整日期(或一组直接作为你做的时间价值)所需的偏移。然后使用UTC的方法来读出的日期和时间值。

例如。找到UTC等效时间+ 05:30你可以这样做:

\r
\r

函数toLocalISO(D){\r
  函数z(N){返回(N小于10'0':'')+ N}\r
  返回d.getUTCFullYear()+' - '+ Z(d.getUTCMonth()+ 1)+' - '+ Z(d.getUTCDate())+'T'+\r
         Z(d.getUTCHours())+:+ Z(d.getUTCMinutes())+:+ Z(d.getUTCSeconds())\r
    \r
}\r
\r
变种D =新的日期(2016-04-10T09:00:00.0');\r
的document.write('当它\\的:'+ D +'此处< BR>');\r
d.setUTCMinutes(d.getUTCMinutes()+ 330);\r
的document.write('它\\:'+ toLocalISO(D)+'在UTC + 05:30');

\r

\r
\r

I have date time below conversion logic in Javascript which is converting the UTC time into passed timezone's local time. I am wondering this logic will work fine during Daylight transition? If not what is the remedy for that? I can't use any third party library. I have to use pure javascript or Angular JS.

 function myTime()
        {
        
        var d1= document.getElementById("txtDate").value;
        var zOffset = document.getElementById("txtOffset").value;
        console.log("Date1",d1);
        
        var d2 = new Date(d1.replace(/ /g,'T'));
        
        var d3= d2.getTime()+(d2.getTimezoneOffset()*60000);
        
        console.log("Date2",d2);
        console.log("Date3",d3);
        var d4 = new Date(d3 + (3600000 * zOffset));
        
        console.log("Date3",d3);
        console.log("Date 4",d4);
        
        var d5 = d4.toLocaleTimeString();
        console.log("Date 5",d5);
        
        //d6 =d5.match(/(\d+)(?=:\d+:\d+)|([A-Z]+)(?=$)/g).join(" ");
        
        //console.log("Date6",d6)
        
        document.getElementById("hourValue").innerHTML = d5;
        }

<h1>
TimeZone
</h1>
<h2 id="hourValue">

</h2>
<table>
<tr>
  <td>Date Time</td>
  <td><input type="text" id="txtDate" value="2016-04-10 09:00:00.0" /></td>
</tr>
<tr><td>TimeZone</td></tr>
<tr><td><input type="text" id="txtOffset" value="-5.00" /></td></tr>
<tr><td><input type="submit" id="btnSubmit" value="Convert" onClick="myTime()" /></td></tr>
</table>

解决方案

getTimezoneOffset simply gets the offset from the host system, so it is as "stable" as the host.

Note: parsing of strings with the Date constructor is almost entirely implementation dependent and very unreliable, just don't do it. Always manually parse strings. A library can help, but a parsing function for a particular format is 2 lines (3 with validation).

When you change "2016-04-10 09:00:00.0" to "2016-04-10T09:00:00.0" you have created an ISO 8601 date and time string without a timezone. If parsed correctly (about 10% of browsers in use won't parse it at all), it should be treated as a local date and time (Chrome treats it incorrectly as UTC, IE 11 correctly as local), so the host system settings will be used to adjust the created date's UTC time value so that it represents the same moment in time.

e.g.

alert(new Date("2016-04-10T09:00:00.0"));

should print a date for 10 April, 2016 09:00 am regardless of the host system's timezone offset (but not in browsers that treat it as UTC, which is wrong).

If you want to find out the equivalent time in some other time zone, use UTC methods to adjust the Date (or set the time value directly as you've done) for the desired offset. Then use UTC methods to read the date and time values.

e.g. to find the equivalent time in UTC+05:30 you might do:

function toLocalISO(d){
  function z(n){return (n<10?'0':'') + n}
  return d.getUTCFullYear() + '-' + z(d.getUTCMonth()+1) + '-' + z(d.getUTCDate()) + 'T' +
         z(d.getUTCHours()) + ':' + z(d.getUTCMinutes()) + ':' + z(d.getUTCSeconds())
    
}

var d = new Date('2016-04-10T09:00:00.0');
document.write('When it\'s : ' + d + ' here<br>');
d.setUTCMinutes(d.getUTCMinutes() + 330); 
document.write('It\'s : ' + toLocalISO(d) + ' at UTC+05:30');

这篇关于是使用getTimezoneOffset()夏令过渡时期稳定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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