Moment Timezone在加载时返回Uncaught TypeError

kfh*_*ohn 6 javascript timezone typeerror requirejs momentjs

我正在努力将Moment Timezone实现到Django应用程序中,以便纠正从不同时区访问它的用户,并且在通过Require.js导入文件时遇到错误.moment.js,moment-timezone.js和moment-timezone-data.js都在加载,但是当我的脚本运行并尝试启动它们时,moment-timezone.js和moment-timezone-data.js会抛出Uncaught TypeErrors.

我的moment-timezone-data.js文件是从Moment.js时区数据生成器复制粘贴的,看起来像这样(虽然有更多的时区):

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": {}
});
Run Code Online (Sandbox Code Playgroud)

requireConfig文件设置如下:

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"]
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

然后我尝试像这样启动Moment Timezone:

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

moment-timezone-data.js在第1行抛出Uncaught TypeError"无法调用方法'添加'未定义":

moment.tz.add({ ... });
Run Code Online (Sandbox Code Playgroud)

moment-timezone.js在第308行抛出一个Uncaught TypeError"无法调用未定义的方法'规则":

return [zone, zone.rule(mom, lastZone)];
Run Code Online (Sandbox Code Playgroud)

qui*_*int 11

你的define()电话只需要moment-timezonemoment-timezone-data.从本质上讲,moment-timezone它是一种替代品moment,可以扩展它以提供.tz().参考示例:

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

此外,您不需要填充时区数据.相反,只需在使用时区数据构建器时选择"AMD"选项.