Moment with Timezone not properly formatting date

757 views Asked by At

I'm attempting to format 2014-10-03T23:09:42.764Z using Moment with Timezones so that I can properly display it for the app's users. However, no matter what timezone I input into Moment it still keeps displaying the current locale's timezone: (I'm in EST)

moment('2014-10-03T23:09:42.764Z').tz('Europe/Paris').format()
"2014-10-03T19:09:42-04:00"
moment('2014-10-03T23:09:42.764Z').tz('Europe/London').format()
"2014-10-03T19:09:42-04:00"

Even though I input both London and Paris, it still displays it in my current timezone. Is there a way to force Moment to display in the given timezone?

2

There are 2 answers

1
Matt Johnson-Pint On BEST ANSWER

Your code looks correct, and it works when testing in the Chrome dev tools debugger console while on the moment-timezone website:

Screenshot

Here's a working snippet:

var a = moment('2014-10-03T23:09:42.764Z').tz('Europe/Paris').format();
var b = moment('2014-10-03T23:09:42.764Z').tz('Europe/London').format();

document.getElementById("a").innerHTML = a;
document.getElementById("b").innerHTML = b;
<script src="http://momentjs.com/downloads/moment.min.js"></script>
<script src="http://momentjs.com/downloads/moment-timezone-with-data-2010-2020.min.js"></script>
<div id="a"></div>
<div id="b"></div>

I can only conclude that you have not loaded the moment-timezone data correctly.

5
Tony On

You should try something like :

var m = moment.tz('2014-10-03T23:09:42.764Z', 'America/Toronto');    
m.tz("Europe/Paris").format();

to set the default timezone, in your post:

var date = moment('2014-10-03T23:09:42.764Z')

it seems, there is no default time zone information in the date object.

You can see the official documentation to have more detail.

I hope it may help