Javascript Intl.DateTimeFormat生成错误的格式
在当前的Chrome浏览器中,Firefox和SafariJavascript Intl.DateTimeFormat生成错误的格式,javascript,internationalization,react-intl,Javascript,Internationalization,React Intl,在当前的Chrome浏览器中,Firefox和SafariIntl.DateTimeFormat对于localeit CH(瑞士的意大利部分)是错误的: new window.Intl.DateTimeFormat('it-CH').format(new Date()) // -> "6/7/2017" new window.Intl.DateTimeFormat('fr-CH').format(new Date()) // -> "06.07.2017" new window.In
Intl.DateTimeFormat
对于localeit CH
(瑞士的意大利部分)是错误的:
new window.Intl.DateTimeFormat('it-CH').format(new Date()) // -> "6/7/2017"
new window.Intl.DateTimeFormat('fr-CH').format(new Date()) // -> "06.07.2017"
new window.Intl.DateTimeFormat('de-CH').format(new Date()) // -> "06.07.2017"
第一行的输出错误。在瑞士各地,格式应为“dd.mm.yyyy”
有趣的是,IE11和Edge为上述代码段生成了正确的输出
修复/修补/覆盖给定浏览器中错误的window.Intl实现的最佳方法是什么?不确定最佳方法,但最简单的方法应该是这样的:
var nativeDateTimeFormat = window.Intl.DateTimeFormat;
window.Intl.DateTimeFormat = function(locale) {
var native = nativeDateTimeFormat(locale);
if (locale === 'it-CH') {
native.format = function() {
return nativeDateTimeFormat('fr-CH').format();
}
}
return native;
}
此解决方案使用了一个事实,即
fr CH
具有it CH
应该具有的正确格式。不幸的是,这在Chrome中似乎不起作用。在本机DateTimeFormat
上设置format
函数似乎是不可能的。设置它没有任何效果……我在这里发布了一个后续问题:@jbandi啊,我明白了。希望您可以在实用函数中使用我建议的代码并使用它,但是如果您依赖于在内部使用Intl.DateTimeFormat
的第三方库,那么很遗憾,这当然不是一个选项。