我正在努力将 Moment Timezone 实现到 Django 应用程序中,以便纠正从不同时区访问它的用户,并且在通过 Require.js 导入文件时遇到错误。 moment.js、moment-timezone.js 和 moment-timezone-data.js 都在加载,但是当我的脚本运行并尝试启动它们时,moment-timezone.js 和 moment-timezone-data.js 会抛出未捕获的类型错误。
我的 moment-timezone-data.js 文件是从Moment.js 时区数据生成器 http://momentjs.com/timezone/data/看起来像这样(尽管有更多时区):
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 行抛出“无法调用未定义的方法‘add’”的未捕获类型错误:
moment.tz.add({ ... });
moment-timezone.js 在第 308 行抛出“无法调用未定义的方法‘规则’”的 Uncaught TypeError:
return [zone, zone.rule(mom, lastZone)];