Dates in Safari appear off by one using Intl.DateTimeFormat with en-US locale

大憨熊 提交于 2021-01-29 17:52:25

问题


It looks like Safari's implementation of Int.DateTimeFormat assumes that the second Sunday in March will ALWAYS be the DST time cutoff which is not correct as prior to 2007 it was the first Sunday in April. It also appears that this affects the other end as well when DST ends. PS: This code is being run in Indiana, USA which is in the eastern time zone (GMT-4)

More specifically...

  • 2007 and newer: correctly for all dates.
  • 2006: incorrectly for dates between the second Sunday in March and first Sunday in April, and between the last Sunday in October and the first Sunday in November.
  • 2005 and older: incorrectly for all dates between the second Sunday in March and first Sunday in November.

Here's a little JSBin that outlines the exact dates where this becomes an issue (note it all works correctly on every browser but safari) https://jsbin.com/mayadehowu/edit?js,output

var formatter = new Intl.DateTimeFormat('en');
var date = new Date('6/2/2005');
console.log(formatter.format(date)); // => outputs "6/1/2005"

I dug in further and it may be due to this change in the ECMA specifications.

  • ES5.1 https://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.8 "whether daylight saving time would have been in effect if the current daylight saving time algorithm had been used at the time"
  • ES6 https://www.ecma-international.org/ecma-262/6.0/#sec-daylight-saving-time-adjustment "An implementation of ECMAScript is expected to make its best effort to determine the local daylight saving time adjustment. NOTE It is recommended that implementations use the time zone information of the IANA Time Zone Database"

Has anyone else encountered this problem? If so what were your solutions to work around this? I need a fix, but I am skeptical to patch over a safari-specific bug by adding an hour in specific cases because if Safari fixes this then our logic will be broken again.

来源:https://stackoverflow.com/questions/56242641/dates-in-safari-appear-off-by-one-using-intl-datetimeformat-with-en-us-locale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!