IE 11抛出“'timeZone'超出有效范围”,当将时区设置为“欧洲/伦敦”时, [英] IE 11 throwing "'timeZone' is outside of valid range" when setting timezone to "Europe/London"

查看:227
本文介绍了IE 11抛出“'timeZone'超出有效范围”,当将时区设置为“欧洲/伦敦”时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些js可以将当地时间显示在英国的时区中。

I have some js to get the local time displayed in uk's timezone.

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以外的所有浏览器中都可以正常工作(叹息。 ),则会引发以下错误:

Works fine in all browsers except IE 11(sigh...) which throws the following error:

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

有人有任何建议吗?参见 http://jsbin.com/dawaqocuvi/1/edit?html, js,控制台,输出,例如支持IE的小提琴。

Does anybody have any suggestions? See http://jsbin.com/dawaqocuvi/1/edit?html,js,console,output for an example fiddle that supports IE.

推荐答案

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

IE11, has limited support of date formatting when using the timezone option, basically the only valid option is UTC. Not very useful.

我可以考虑的两个选择。

Two options that I can think off.


  1. 使用momentjs,这是一个不错的日期时间库,但可能会过大。

  2. 使用pollyfill

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

Below is a snippet, that polyfill's the locale timezone, and creates a simple world time clock, with a few zones..

如果您运行的是现代浏览器,则应该

If your running a modern browser, you should be able to remove the script tag at the start and it will continue to work.


更新:现在可以在IE11中正常工作了,大小写很重要使用polyfill进行语言环境时。例如。 欧洲/阿姆斯特丹很好,但是欧洲大陆/阿姆斯特丹不好,对于Chrome来说似乎并不重要。 / p>

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>

这篇关于IE 11抛出“'timeZone'超出有效范围”,当将时区设置为“欧洲/伦敦”时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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