date.toLocaleDateString(locale,{timeZone:'Asia/Kolkata'})在in 11中不起作用 [英] date.toLocaleDateString(locale,{timeZone:'Asia/Kolkata'}) not working in ie 11

查看:104
本文介绍了date.toLocaleDateString(locale,{timeZone:'Asia/Kolkata'})在in 11中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用时区和语言环境转换日期和时间,但是在IE11中不起作用.它可以在chrome,firefox和edge中工作.

I am trying to convert date and time with timezone and locale but, it is not working in IE11. It is working in chrome, firefox, and edge.

至少完整的可验证示例:

function getLocalTime(date){
    var timeZone = "Asia/Kolkata";
    var utcDate =  new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()));
    var dateAndTime = utcDate.toLocaleDateString("en",{timeZone:timeZone})+" "+ utcDate.toLocaleTimeString("en",{timeZone:timeZone}).replace(/:\d+ /, ' ');;
    return dateAndTime;
}

console.log(getLocalTime(Date());

IE11中的错误:

SCRIPT5118:"timeZone"的选项值"ASIA/KOLKATA"超出有效范围.预期:['UTC']

注意:我不想使用任何第三方js.

Note: I don't want to use any third party js.

推荐答案

有关MDN的timeZone文档说,对时区字符串的支持是可选的,但"UTC"除外:

timeZone documentation on MDN says that support for timezone strings is optional, except for "UTC":

实现必须识别的唯一值是"UTC";默认值为运行时的默认时区.实施还可以识别IANA时区数据库的时区名称,例如亚洲/上海",亚洲/加尔各答",美国/纽约".

The only value implementations must recognize is "UTC"; the default is the runtime's default time zone. Implementations may also recognize the time zone names of the IANA time zone database, such as "Asia/Shanghai", "Asia/Kolkata", "America/New_York".

ECMAScript规范的相关部分对此进行确认(@的技巧提示mkaatman):

Relevant portions of the ECMAScript spec confirm this (hat tip to @mkaatman):

[[timeZone]]是字符串值"UTC",或者是或未定义.

[[timeZone]] is either the String value "UTC" or undefined.

对于DateTimeFormat构造函数中的options属性timeZone,允许符合标准的实现接受其他值,然后具有实现定义的行为,而不是引发RangeError,前提是必须接受其他可接受的输入值IANA时区数据库中区域或链接标识符的大小写不区分大小写匹配,并且对于DateTimeFormat.resolvedOptions返回的对象的timeZone属性,被规范化为数据库中使用的大小写中的区域标识符,但"Etc/GMT"除外.应该规范化为"UTC".

A conforming implementation is permitted to accept additional values, and then have implementation-defined behaviour instead of throwing a RangeError, for [...t]he options property timeZone in the DateTimeFormat constructor, provided that the additional acceptable input values are case-insensitive matches of Zone or Link identifiers in the IANA time zone database and are canonicalized to Zone identifiers in the casing used in the database for the timeZone property of the object returned by DateTimeFormat.resolvedOptions, except that "Etc/GMT" shall be canonicalized to "UTC".

Microsoft文档指出它们开始在IE11中支持localeDateString:

Microsoft's documentation states they started supporting localeDateString in IE11:

从Internet Explorer 11开始,toLocaleDateString在内部使用Intl.DateTimeFormat来格式化日期,从而增加了对语言环境和选项参数的支持.

Starting in Internet Explorer 11, toLocaleDateString uses Intl.DateTimeFormat internally to format the date, which adds support for the locales and options parameters.

...但是根据您的错误消息,他们的支持是针对最小的'UTC'或未定义"版本.时区数据库位于此处,以了解其价值,但是看起来您需要找到一个

...but based on your error message their support is for the minimal "'UTC' or undefined" version. The timezone database is here for what it's worth, but it looks like you'll need to find a different way to handle this (probably involving getTimezoneOffset() rather than named timezones.)

这篇关于date.toLocaleDateString(locale,{timeZone:'Asia/Kolkata'})在in 11中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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