该测试是在 Chrome 53 中进行的。
我添加了一些选项DateTimeFormat
检查日期的其他字段:
options = {
year: 'numeric', month: 'numeric', day: 'numeric',
hour: 'numeric', minute: 'numeric', second: 'numeric',
hour12: false, timeZoneName: 'long'
};
var workingDate = Intl.DateTimeFormat('en-GB', options).format(new Date('Fri May 11 1848 01:00:00 GMT+0100 (BST)'));
var notWorkingDate = Intl.DateTimeFormat('en-GB', options).format(new Date('Fri May 11 1847 01:00:00 GMT+0100 (BST)'));
结果是:
workingDate: 10/05/1848, 20:53:32 GMT-03:06:28
notWorkingDate: 10/05/1847, 20:53:32 GMT-03:06:28
大多数地方在 1900 年之前都没有标准化的基于 UTC 的偏移量(实际上,每个国家在不同的年份采用了它),所以在 1900 年之前你总是会得到那些奇怪的结果。其实,作为马特在评论中解释道 https://stackoverflow.com/questions/45512717/intl-datetimeformat-gives-strange-result-from-year-1847-or-below#comment77993142_45513361,UTC 于 1972 年实施,在此之前,大多数区域被定义为与 GMT 的偏移。不管怎样,对于非常古老的日期,特别是 1900 年之前,您可能会期望像上面这样的偏移量.
在这种情况下,它会获取系统默认时区的相应偏移量(America/Sao_Paulo
):1914 年之前是-03:06:28
.
在伦敦(我假设这是您的默认时区),在 1847 年 12 月 1 日之前,偏移量为-00:01:15 https://www.timeanddate.com/time/zone/uk/london?year=1800#tb-zone(按纬度/经度计算,再次参见马特的评论 https://stackoverflow.com/questions/45512717/intl-datetimeformat-gives-strange-result-from-year-1847-or-below#comment77993142_45513361了解更多详情),之后更改为+00:00
(这就是为什么它适用于 1848 年的日期)。
我做了一个测试,将时区设置为Europe/London
:
options = {
year: 'numeric', month: 'numeric', day: 'numeric',
hour: 'numeric', minute: 'numeric', second: 'numeric',
hour12: false, timeZoneName: 'long', timeZone: 'Europe/London'
};
var workingDate = Intl.DateTimeFormat('en-GB', options).format(new Date('Fri May 11 1848 01:00:00 GMT+0100 (BST)'));
var notWorkingDate = Intl.DateTimeFormat('en-GB', options).format(new Date('Fri May 11 1847 01:00:00 GMT+0100 (BST)'));
结果是:
1848 年 11 月 5 日,00:00:00 GMT
1847 年 10 月 5 日,23:58:45 GMT-00:01:15
这证实了在 1847 年 12 月之前,日期有不同的偏移量。
解决此问题的一种方法是将日期视为 UTC:
options = {
timeZone: 'UTC'
};
var workingDate = Intl.DateTimeFormat('en-GB', options).format(new Date('Fri May 11 1848 01:00:00 GMT+0100 (BST)'));
var notWorkingDate = Intl.DateTimeFormat('en-GB', options).format(new Date('Fri May 11 1847 01:00:00 GMT+0100 (BST)'));
这些值将是:
1848 年 11 月 5 日
1847 年 11 月 5 日