Moment Timezone return Uncaught TypeError on boot

I am working on implementing Moment Timezone in a Django application to fix user access to it from different time zones, and I am launching an error when importing files through Require.js. time.js, moment-timezone.js and time-timezone-data.js are all loaded, but when my script starts and tries to initiate them, moment-moment zone.js and moment-time-data .js throw Unopened TypeErrors.

My time-timezone-data.js file is copied from the Moment.js time zone data generator and looks like this (although with more time zones):

moment.tz.add({ "zones": { "America/New_York": [ "-4:56:2 - LMT 1883_10_18_12_3_58 -4:56:2", "-5 US E%sT 1920 -5", "-5 NYC E%sT 1942 -5", "-5 US E%sT 1946 -5", "-5 NYC E%sT 1967 -5", "-5 US E%sT" ] }, "rules": { "US": [ "1918 1919 2 0 8 2 0 1 D", "1918 1919 9 0 8 2 0 0 S", "1942 1942 1 9 7 2 0 1 W", "1945 1945 7 14 7 23 1 1 P", "1945 1945 8 30 7 2 0 0 S", "1967 2006 9 0 8 2 0 0 S", "1967 1973 3 0 8 2 0 1 D", "1974 1974 0 6 7 2 0 1 D", "1975 1975 1 23 7 2 0 1 D", "1976 1986 3 0 8 2 0 1 D", "1987 2006 3 1 0 2 0 1 D", "2007 9999 2 8 0 2 0 1 D", "2007 9999 10 1 0 2 0 0 S" ], "NYC": [ "1920 1920 2 0 8 2 0 1 D", "1920 1920 9 0 8 2 0 0 S", "1921 1966 3 0 8 2 0 1 D", "1921 1954 8 0 8 2 0 0 S", "1955 1966 9 0 8 2 0 0 S" ] }, "links": {} }); 

The requireConfig file is configured as follows:

 require = { paths: { "moment": ServerInfo.generateStaticPathFor("js/ext/moment/moment-with-langs"), "moment-timezone": ServerInfo.generateStaticPathFor("js/ext/moment/moment-timezone"), "moment-timezone-data": ServerInfo.generateStaticPathFor("js/ext/moment/moment-timezone-data") }, shim: { "moment-timezone-data": { "deps": ["moment-timezone"] } } }; 

Then I try to start Moment Timezone as follows:

 define(["moment", "moment-timezone", "moment-timezone-data"], function(moment) { var thisMoment = moment().tz('America/New_York').startOf('day'); }); 

moment-timezone-data.js raises an Uncaught TypeError on "Unable to call the add method from undefined on line 1:

 moment.tz.add({ ... }); 

moment-timezone.js gives the error message "Unable to use TypeError" from the rule "Unable to call the" undefined "method on line 308:

 return [zone, zone.rule(mom, lastZone)]; 
+6
source share
4 answers

Calling define() requires only moment-timezone and moment-timezone-data . Essentially, moment-timezone acts as a replacement for replacing moment , expanding it to provide .tz() . See an example :

 define(["moment-timezone", "moment-timezone-data"], function (moment) { moment().tz("America/Los_Angeles").format(); }); 

In addition, you do not need to loop the time zone data. Instead, simply select the "AMD" option when using the time zone data creator .

+11
source

It doesn't matter if you change the order of the dependencies? I believe that the moment-time depends on the data of the moment-time, and not vice versa. But I'm not sure if this matters here or not.

+1
source

I was getting this error when I made a typo in the time zone.

eg. moment().tz("America/Los_Anegles").format();

+1
source

I was getting this problem when using the shortened version of moment-timezone.js under cdnjs . Just changed to full source, loaded with instant time zone and it works!

 (function($) { moment.tz.add({ "zones": { "America/Mexico_City": [ "-6:36:36 - LMT 1922_0_1_0_23_24 -6:36:36", "-7 - MST 1927_5_10_23 -7", "-6 - CST 1930_10_15 -6", "-7 - MST 1931_4_1_23 -7", "-6 - CST 1931_9 -6", "-7 - MST 1932_3_1 -7", "-6 Mexico C%sT 2001_8_30_02 -5", "-6 - CST 2002_1_20 -6", "-6 Mexico C%sT" ] }, "rules": { "Mexico": [ "1939 1939 1 5 7 0 0 1 D", "1939 1939 5 25 7 0 0 0 S", "1940 1940 11 9 7 0 0 1 D", "1941 1941 3 1 7 0 0 0 S", "1943 1943 11 16 7 0 0 1 W", "1944 1944 4 1 7 0 0 0 S", "1950 1950 1 12 7 0 0 1 D", "1950 1950 6 30 7 0 0 0 S", "1996 2000 3 1 0 2 0 1 D", "1996 2000 9 0 8 2 0 0 S", "2001 2001 4 1 0 2 0 1 D", "2001 2001 8 0 8 2 0 0 S", "2002 9999 3 1 0 2 0 1 D", "2002 9999 9 0 8 2 0 0 S" ] }, "links": {} }); console.log(moment().tz("America/Mexico_City").format()); })(jQuery); 
0
source

All Articles