【问题标题】:Moment Timezone returning Uncaught TypeError on load时刻时区在加载时返回 Uncaught TypeError
【发布时间】:2014-01-21 15:38:54
【问题描述】:

我正在努力在 Django 应用程序中实现 Moment Timezone,以纠正从不同时区访问它的用户,并且在通过 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 timezone data generator 复制粘贴的,看起来像这样(尽管有更多时区):

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": {}
});

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

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

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

moment-timezone-data.js 在第 1 行抛出“Cannot call method 'add' of undefined”的 Uncaught TypeError:

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

moment-timezone.js 在第 308 行抛出“无法调用未定义的方法‘规则’”的 Uncaught TypeError:

return [zone, zone.rule(mom, lastZone)];

【问题讨论】:

    标签: javascript timezone requirejs typeerror momentjs


    【解决方案1】:

    如果切换依赖顺序有什么不同吗?我相信时刻时区取决于时刻时区数据,而不是相反。但我不确定这是否重要。

    【讨论】:

      【解决方案2】:

      您的define() 呼叫只需要moment-timezonemoment-timezone-data。从本质上讲,moment-timezone 就像moment 的直接替代品,将其扩展为提供.tz()。参考the example

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

      另外,您不需要填充时区数据。相反,在使用the timezone data builder 时只需选择“AMD”选项。

      【讨论】:

      • 我认为 AMD 选项在很大程度上起到了作用。谢谢!
      • 这里也是如此.. 12 周后,就在放弃时刻 js 之前。选择“AMD”确实有效
      【解决方案3】:

      我在时区输入错误时收到此错误。

      例如。 moment().tz("America/Los_Anegles").format();

      【讨论】:

        【解决方案4】:

        我在使用cdnjs 下的 moment-timezone.js 的缩小版本时遇到了这个问题。 刚改成从moment timezone website下载的完整源代码就可以了!

        (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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-05-11
          • 1970-01-01
          • 2018-11-08
          • 1970-01-01
          • 2017-03-08
          • 1970-01-01
          • 2022-06-11
          相关资源
          最近更新 更多