IE 11 throwing “'timeZone' is outside of valid range” when setting timezone to “Europe/London”

陌路散爱 提交于 2020-06-14 06:52:23

问题


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)

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

Option value 'EUROPE/LONDON' for 'timeZone' is outside of valid range. Expected: ['UTC']

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


回答1:


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. Use momentjs, this is a nice datetime lib, but might be overkill
  2. Use a pollyfill

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.

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>



回答2:


Yes, as stated above, IE 11 supports most of the Intl API but does not support the use of a specific timeZone: http://kangax.github.io/compat-table/esintl/#test-DateTimeFormat_accepts_IANA_timezone_names

Here is an example of how to polyfill just the Intl.DateTimeFormat function's timeZone support using webpack's require.ensure:

const fillIntlDateTimeFormatTimezone = () => new Promise((resolve) => {
    try {
        new Intl.DateTimeFormat('en', {
            timeZone: 'America/Los_Angeles',
            timeZoneName: 'long'
        }).format();
        return resolve();
    } catch (error) {
        require.ensure(['date-time-format-timezone'], (require) => {
            require('date-time-format-timezone');
            return resolve();
        }, 'DateTimeFormatTimezone');
    }
});

This should only load the polyfill for browsers that don't support specifying a timeZone property which should only be IE 11 at this point.



来源:https://stackoverflow.com/questions/54364061/ie-11-throwing-timezone-is-outside-of-valid-range-when-setting-timezone-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!