如何加载时区数据到Moment-timezone.js
How to load time zone data into Moment-timezone.js
我正在努力加载时区数据以供 moment-timezone.js 使用。下面是我的代码(为简洁起见,我只添加了 2 个区域,但确实需要添加更多):
<script src='/js/jquery-1.11.0.min.js' type='text/javascript'></script>
<script src="/s/js/moment.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone-with-data-2010-2020.js" type="text/javascript"></script>
<script>
moment.tz.add([
"America/Chicago|CST CDT EST CWT CPT|60 50 50 50 50|01010101010101010101010101010101010102010101010103401010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261s0 1nX0 11B0 1nX0 1wp0 TX0 WN0 1qL0 1cN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 11B0 1Hz0 14p0 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 RB0 8x30 iw0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 s10 1Vz0 LB0 1BX0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0",
"America/New_York|EST EDT EWT EPT|50 40 40 40|01010101010101010101010101010101010101010101010102301010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261t0 1nX0 11B0 1nX0 11B0 1qL0 1a10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 RB0 8x40 iv0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 s10 1Vz0 LB0 1BX0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0"
]);
var userTimeZone = document.getElementById("dom-target-usertimezone");
var deviceTimeZone = document.getElementById("dom-target-devicetimezone");
console.debug("User timezone: " + userTimeZone.textContent);
console.debug("Device timezone: " + deviceTimeZone.textContent);
console.debug("Loaded tz names: " + moment.tz.names());
var deviceNow = moment.tz(moment().utc().valueOf(),deviceTimeZone.textContent);
console.debug("User: " + moment(deviceNow).format());
var time = moment(deviceNow).format('h:mmA');
console.debug("time = " + time);
</script>
我得到以下输出
User timezone:
America/Chicago
Device timezone:
America/New_York
Loaded tz names: America/Chicago,America/New_York
Moment Timezone has no data for
America/New_York. See http://momentjs.com/timezone/docs/#/data-loading/.
User: 2015-04-26T12:38:02+00:00
time = 12:38PM
因此,如您所见,moment.tz.names() 表示我加载了纽约,但是当我尝试将 UTC 时区调整为纽约时,我被告知没有数据。帮助。谢谢
解决方案:
<script src='/js/jquery-1.11.0.min.js' type='text/javascript'></script>
<script src="/s/js/moment.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone-with-data-2010-2020.min.js" type="text/javascript"></script>
var deviceTimeZone = document.getElementById("dom-target-devicetimezone").textContent.trim();
var deviceNow = moment.tz(deviceTimeZone);
var time = deviceNow.format('h:mmA');
document.getElementById('clock').innerHTML = time;
您加载了两次moment-timezone,加载了两次数据。估计是糊涂了。
<script src="/s/js/moment-timezone.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone-with-data-2010-2020.js" type="text/javascript">
以上两条语句都加载了moment-timezone代码。你应该只有其中之一。
如果您使用 moment.tz.add
加载特定区域,那么您应该使用 moment-timezone
脚本。如果您想包括所有区域,请使用 moment-timezone-with-data
脚本之一。
不要加载 moment-timezone-with-data
然后使用 moment.tz.add
,因为那样只会复制数据文件中已有的区域信息。
此外,您的代码可以简化:
var deviceNow = moment.tz(deviceTimeZone.textContent);
console.debug("User: " + deviceNow.format());
var time = deviceNow.format('h:mmA');
console.debug("time = " + time);
我不确定你为什么要用两种不同的方式对它进行两次格式化,但这段代码等同于你写的。
我正在努力加载时区数据以供 moment-timezone.js 使用。下面是我的代码(为简洁起见,我只添加了 2 个区域,但确实需要添加更多):
<script src='/js/jquery-1.11.0.min.js' type='text/javascript'></script>
<script src="/s/js/moment.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone-with-data-2010-2020.js" type="text/javascript"></script>
<script>
moment.tz.add([
"America/Chicago|CST CDT EST CWT CPT|60 50 50 50 50|01010101010101010101010101010101010102010101010103401010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261s0 1nX0 11B0 1nX0 1wp0 TX0 WN0 1qL0 1cN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 11B0 1Hz0 14p0 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 RB0 8x30 iw0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 s10 1Vz0 LB0 1BX0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0",
"America/New_York|EST EDT EWT EPT|50 40 40 40|01010101010101010101010101010101010101010101010102301010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261t0 1n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 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0"
]);
var userTimeZone = document.getElementById("dom-target-usertimezone");
var deviceTimeZone = document.getElementById("dom-target-devicetimezone");
console.debug("User timezone: " + userTimeZone.textContent);
console.debug("Device timezone: " + deviceTimeZone.textContent);
console.debug("Loaded tz names: " + moment.tz.names());
var deviceNow = moment.tz(moment().utc().valueOf(),deviceTimeZone.textContent);
console.debug("User: " + moment(deviceNow).format());
var time = moment(deviceNow).format('h:mmA');
console.debug("time = " + time);
</script>
我得到以下输出
User timezone:
America/Chicago
Device timezone:
America/New_York
Loaded tz names: America/Chicago,America/New_York
Moment Timezone has no data for
America/New_York. See http://momentjs.com/timezone/docs/#/data-loading/.
User: 2015-04-26T12:38:02+00:00
time = 12:38PM
因此,如您所见,moment.tz.names() 表示我加载了纽约,但是当我尝试将 UTC 时区调整为纽约时,我被告知没有数据。帮助。谢谢
解决方案:
<script src='/js/jquery-1.11.0.min.js' type='text/javascript'></script>
<script src="/s/js/moment.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone-with-data-2010-2020.min.js" type="text/javascript"></script>
var deviceTimeZone = document.getElementById("dom-target-devicetimezone").textContent.trim();
var deviceNow = moment.tz(deviceTimeZone);
var time = deviceNow.format('h:mmA');
document.getElementById('clock').innerHTML = time;
您加载了两次moment-timezone,加载了两次数据。估计是糊涂了。
<script src="/s/js/moment-timezone.js" type="text/javascript"></script>
<script src="/s/js/moment-timezone-with-data-2010-2020.js" type="text/javascript">
以上两条语句都加载了moment-timezone代码。你应该只有其中之一。
如果您使用 moment.tz.add
加载特定区域,那么您应该使用 moment-timezone
脚本。如果您想包括所有区域,请使用 moment-timezone-with-data
脚本之一。
不要加载 moment-timezone-with-data
然后使用 moment.tz.add
,因为那样只会复制数据文件中已有的区域信息。
此外,您的代码可以简化:
var deviceNow = moment.tz(deviceTimeZone.textContent);
console.debug("User: " + deviceNow.format());
var time = deviceNow.format('h:mmA');
console.debug("time = " + time);
我不确定你为什么要用两种不同的方式对它进行两次格式化,但这段代码等同于你写的。