Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment Timezone returning Uncaught TypeError on load

I am working on implementing Moment Timezone into a Django application in order to correct for users accessing it from different time zones, and I am running into an error when importing the files through Require.js. moment.js, moment-timezone.js, and moment-timezone-data.js are all loading, but when my script runs and tries to initiate them, moment-timezone.js and moment-timezone-data.js throw Uncaught TypeErrors.

My moment-timezone-data.js file is copy-pasted from the Moment.js timezone data generator and looks like this (albeit 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 set up like so:

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"]
        }
    }
};

I then try to initiate Moment Timezone like so:

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

moment-timezone-data.js throws an Uncaught TypeError of "Cannot call method 'add' of undefined" on line 1:

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

moment-timezone.js throws an Uncaught TypeError of "Cannot call method 'rule' of undefined" on line 308:

return [zone, zone.rule(mom, lastZone)];
like image 264
kfhohn Avatar asked Jan 03 '14 21:01

kfhohn


2 Answers

Your define() call only needs moment-timezone and moment-timezone-data. Essentially, moment-timezone acts like a drop-in replacement for moment, extending it to provide .tz(). Refer to the example:

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

Also, you don't need to shim the timezone data. Instead, just select the "AMD" option when using the timezone data builder.

like image 65
quietmint Avatar answered Oct 19 '22 23:10

quietmint


Does it make any difference if you switch the dependency order? I believe moment-timezone depends on moment-timezone-data, not the other way around. But I'm not sure if it matters here or not.

like image 44
Matt Johnson-Pint Avatar answered Oct 19 '22 23:10

Matt Johnson-Pint