gpt4 book ai didi

javascript - 将时区设置为 "' 时,IE 11 抛出 "Europe/London"timeZone' is outside of valid range"

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:09:05 25 4
gpt4 key购买 nike

我有一些 js 可以让本地时间显示在英国时区。

var d = new Date(); // local system time (whatever timezone that is)
var dUK = new Date(d.toLocaleString('en-GB', { timeZone: 'Europe/London' })); // UK timezone (covers BST too)

在所有浏览器中工作正常,除了 IE 11(唉......)会抛出以下错误:

“timeZone”的选项值“EUROPE/LONDON”超出有效范围。预期:['UTC']

有人有什么建议吗?参见 http://jsbin.com/dawaqocuvi/1/edit?html,js,console,output对于支持 IE 的示例 fiddle 。

最佳答案

IE11,在使用时区选项时对日期格式的支持有限,基本上唯一有效的选项是UTC。不是很有用。

我能想到的两个选项。

  1. 使用 momentjs,这是一个很好的日期时间库,但可能有点矫枉过正
  2. 使用 pollyfill

下面是一个片段,polyfill 是语言环境时区,并创建了一个简单的世界时钟,有几个时区..

如果你运行的是现代浏览器,你应该能够在开始时删除脚本标签,它会继续工作。

Update: Got this working now in IE11, case is important when doing the locale with the polyfill. eg. Europe/Amsterdam is fine, but europe/amsterdam is not, with Chrome it didn't seem to matter.

//var d = new Date(); var dUK = d.toLocaleString('en-GB', { timeZone: 'America/Chicago' }); console.log(dUK);

function updateTimes() {
var dt = new Date();
var els = document.querySelectorAll("[data-tz]");
for (var l = 0; l < els.length; l ++) {
var d = els[l];
d.innerText = dt.toLocaleString('en-GB', {timeZone: d.dataset.tz});
}
}

updateTimes();
setInterval(updateTimes, 1000);
<script src="https://unpkg.com/date-time-format-timezone@latest/build/browserified/date-time-format-timezone-complete-min.js"></script>

<p>Simple world time clocks</p>

<div><span data-tz="Europe/London"></span>
- London
</div>
<div>
<span data-tz="Europe/Amsterdam"></span>
- Amsterdam
</div>
<div>
<span data-tz="America/Chicago"></span>
- Chicago
</div>

关于javascript - 将时区设置为 "' 时,IE 11 抛出 "Europe/London"timeZone' is outside of valid range",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54364061/

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