在chrome中的js日期对象中奇怪的秒数偏移 [英] weird seconds offset in js date object in chrome

查看:142
本文介绍了在chrome中的js日期对象中奇怪的秒数偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在一年的开始时查看日期对象的valueOf值时,我预计总是会收到零秒。
以下代码显示,直到1917年铬的偏移量为54秒或40秒。在IE中我所有年份都收到0秒。



这是否有原因?它似乎只发生在最后一个chrome版本

  for(var i = 0; i< 2020; i ++)
if(!new Date(i,0,1).valueOf()。toString()。match(00000 $))
console.log({
y:i,
s :new Date(i,0,1).valueOf()。toString()。match(/(\\\ {2})\\\ {3} $ /)[1]})


解决方案


这是不是BUG ..


正如@Krzysztof所指出的,Chrome已经



所有更改现在反映在Chrome中:

  new Date(1941,6,1).toUTCString()
// 1941年6月30日星期一18:06:40 GMT

新日期(1941,11,1).toUTCString()
//Sun,1941年11月30日17:30: 00 GMT

新日期(1942,7,1).toUTCString()
//星期五,1942年7月31日18:30:00 GMT

new Date(1942,11,1).toUTCString()
//Mon,1942年11月30日17:30:00 GMT

所以现在如果我在1941年之前选择任何日期,请记住我的当地时间提前6小时,我看到偏移6分40秒。由于最近Chrome的更新,或者特别说ECMAScript(JavaScript)的更新,它将根据返回日期的时区而有所不同。


When looking at the valueOf value of a date object at the beggining of a year i expected to always receive zero seconds. The following code shows that until 1917 there was an offset of 54 seconds or 40 seconds in chrome. in IE i receive 0 seconds for all years.

Is there a reason for this? it seems to only happen in the last chrome version

   for(var i=0; i<2020;i++)
       if(!new Date(i,0,1).valueOf().toString().match("00000$"))
             console.log({
                    y:i,
                    s: new Date(i,0,1).valueOf().toString().match(/(\d{2})\d{3}$/)[1]})

解决方案

This is Not a BUG..

As @Krzysztof pointed out Chrome has implemented a new spec for timezone offset calculation following the merge of Make LocalTZA take 't' and 'isUTC' and drop DSTA(t) to Ecma 262. So now the time-zone conversion does not work by just backward interval of seconds, it is calculated as what local time was being observed in a specific region.

Explanation:

I am from a wonderful little country called Bangladesh of South-Asia which follows BST(Bangladesh Standard Time +0600 GMT), which was not always exactly 6 hours ahead of GMT. As JavaScript date takes in local time when I print the start time of this year in GMT I get:

new Date(2018, 0, 1).toUTCString()
// "Sun, 31 Dec 2017 18:00:00 GMT"

In 2009 one hour day-light saving was observed in Bangladesh from 19 June to 31 December. So if I print the first day of December 2009 I get:

new Date(2009, 11, 1).toUTCString()
// "Mon, 30 Nov 2009 17:00:00 GMT"

You can see the day-light saving is now reflected in the date now, which is not visible in my nodeJS console. There was also changes in local time in 1941-1942 as shown below and can be seen on timeanddate.com:

All of the changes are reflected in Chrome now:

new Date(1941, 6, 1).toUTCString()
// "Mon, 30 Jun 1941 18:06:40 GMT"

new Date(1941, 11, 1).toUTCString()
// "Sun, 30 Nov 1941 17:30:00 GMT"

new Date(1942, 7, 1).toUTCString()
// "Fri, 31 Jul 1942 18:30:00 GMT"

new Date(1942, 11, 1).toUTCString()
// "Mon, 30 Nov 1942 17:30:00 GMT"

So now if I pick any date before 1941 keeping in mind my local time is 6 hours ahead I see an offset of 6 minutes 40 seconds. It will vary depending on the time-zone for the back dates due to the recent update of Chrome, or specifically saying the update of ECMAScript(JavaScript).

这篇关于在chrome中的js日期对象中奇怪的秒数偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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