Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load time zone data into Moment-timezone.js

I'm struggling to get the timezone data loaded for use by moment-timezone.js. Below is my code (For brevity, I only added 2 zones, but really need to add a lot more):

<script src='/js/jquery-1.11.0.min.js' type='text/javascript'></script>
<script src="/s/js/moment.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone-with-data-2010-2020.js" type="text/javascript"></script>    

<script>

    moment.tz.add([
  "America/Chicago|CST CDT EST CWT CPT|60 50 50 50 50|01010101010101010101010101010101010102010101010103401010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261s0 1nX0 11B0 1nX0 1wp0 TX0 WN0 1qL0 1cN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 11B0 1Hz0 14p0 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 RB0 8x30 iw0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 s10 1Vz0 LB0 1BX0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0",
            "America/New_York|EST EDT EWT EPT|50 40 40 40|01010101010101010101010101010101010101010101010102301010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261t0 1nX0 11B0 1nX0 11B0 1qL0 1a10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 RB0 8x40 iv0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 s10 1Vz0 LB0 1BX0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0"
            ]);

    var userTimeZone = document.getElementById("dom-target-usertimezone");
    var deviceTimeZone = document.getElementById("dom-target-devicetimezone");

    console.debug("User timezone: " + userTimeZone.textContent);
    console.debug("Device timezone: " + deviceTimeZone.textContent);
    console.debug("Loaded tz names: " + moment.tz.names());

    var deviceNow = moment.tz(moment().utc().valueOf(),deviceTimeZone.textContent);
    console.debug("User: " + moment(deviceNow).format());

    var time = moment(deviceNow).format('h:mmA');
    console.debug("time = " + time);    

</script>

I get the following output

User timezone: 
    America/Chicago
Device timezone: 
    America/New_York
Loaded tz names: America/Chicago,America/New_York
Moment Timezone has no data for 
    America/New_York. See http://momentjs.com/timezone/docs/#/data-loading/.
User: 2015-04-26T12:38:02+00:00
time = 12:38PM

So, as you can see, the moment.tz.names() indicates I have New York loaded, yet when I attempt to adjust the UTC time zone to New York, I'm told there's no data. Help. Thanks.

SOLUTION:

<script src='/js/jquery-1.11.0.min.js' type='text/javascript'></script>
<script src="/s/js/moment.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone-with-data-2010-2020.min.js" type="text/javascript"></script>

var deviceTimeZone = document.getElementById("dom-target-devicetimezone").textContent.trim();
var deviceNow = moment.tz(deviceTimeZone);
var time = deviceNow.format('h:mmA');
document.getElementById('clock').innerHTML = time;
like image 213
SteveSTL Avatar asked Dec 24 '22 20:12

SteveSTL


1 Answers

You have loaded moment-timezone twice, and loaded data twice. It's probably getting confused.

<script src="/s/js/moment-timezone.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone-with-data-2010-2020.js" type="text/javascript">

Both of the above statement load the moment-timezone code. You should only have one of them.

If you are loading specific zones using moment.tz.add, then you should use the moment-timezone script. If you want to include all zones, then use one of the moment-timezone-with-data scripts.

Don't load moment-timezone-with-data and then use moment.tz.add, as that just duplicates the zone information that's already in the data file.

Also, your code can be simplified:

var deviceNow = moment.tz(deviceTimeZone.textContent);
console.debug("User: " + deviceNow.format());

var time = deviceNow.format('h:mmA');
console.debug("time = " + time);   

I'm not sure why you wanted it formatted twice in two different ways, but this code is equivalent to what you wrote.

like image 169
Matt Johnson-Pint Avatar answered Jan 09 '23 07:01

Matt Johnson-Pint