我很难将时区数据加载到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;
答案 0 :(得分:10)
您已经加载了两次时间 - 时区,并加载了两次数据。它可能会变得混乱。
<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);
我不确定为什么你希望它以两种不同的方式格式化两次,但是这段代码与你写的相同。