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.
- Use momentjs, this is a nice datetime lib, but might be overkill
- 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>
en-GB
? Also why would you then send that result to anothernew Date(
– Shockheadeden-GB
good spot. I didn't write this - just inherited the error. I've not checked butd
may be getting used further down the line. Anyhow that's not why I've posted the question. – Loyalnew Date(
again, really makes no sense. A locale formatted date is actually an invalid parameter to a date constructor anyway. IOW: It might work, it might not.. Just doing the following works for me ->var d = new Date(); var dUK = d.toLocaleString('en-GB', { timeZone: 'Europe/London' }); console.log(dUK);
– Shockheaded