gpt4 book ai didi

javascript - date.toLocaleDateString(locale,{timeZone :'Asia/Kolkata' }) 在 ie 11 中不起作用

转载 作者:可可西里 更新时间:2023-11-01 02:09:14 24 4
gpt4 key购买 nike

我正在尝试使用时区和区域设置转换日期和时间,但是它在 IE11 中不起作用。它适用于 chrome、firefox 和 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。

最佳答案

timeZone documentation on MDN说对时区字符串的支持是可选的,除了“UTC”:

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 spec 的相关部分确认这一点(向@mkaatman 致敬):

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

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's documentation表示他们开始在 IE11 中支持 localeDateString:

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

...但是根据您的错误消息,他们支持最小的“'UTC' 或未定义”版本。时区数据库是here对于它的值(value),但看起来您需要找到一种不同的方法来处理这个问题(可能涉及 getTimezoneOffset() 而不是命名时区。)

关于javascript - date.toLocaleDateString(locale,{timeZone :'Asia/Kolkata' }) 在 ie 11 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44402560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com