使用 moment.js 设置初始时区
using moment.js to set initial time zone
我尝试按照moment.js初始化指定eastern 8
时区。
我将 os 时区更改为 western 4
,然后我输入与下面相同的时间戳。
moment().utcOffset(-240);
moment(1473998400000).toDate();
moment().utcOffset(480);
moment(1473998400000).toDate();
// both output >>> Fri Sep 16 2016 00:00:00 GMT-0400 (EDT)
但我不明白为什么我从不同的初始时区也得到相同的输出?
我做错了什么:(
更新:
首先,不要使用.toDate()
,否则会得到一个JavaScriptDate
对象,需要自己转换
其次,您需要在初始化的矩对象上调用.utcOffset()
。
moment(1473998400000).utcOffset(-240).format(); // 2016-09-16T00:00:00-04:00
moment(1473998400000).utcOffset(480).format(); // 2016-09-16T12:00:00+08:00
此外,请检查 docs 中的以下摘录:
If you want an actual time zone -- time in a particular location, like
America/Los_Angeles, consider moment-timezone.
我尝试按照moment.js初始化指定eastern 8
时区。
我将 os 时区更改为 western 4
,然后我输入与下面相同的时间戳。
moment().utcOffset(-240);
moment(1473998400000).toDate();
moment().utcOffset(480);
moment(1473998400000).toDate();
// both output >>> Fri Sep 16 2016 00:00:00 GMT-0400 (EDT)
但我不明白为什么我从不同的初始时区也得到相同的输出?
我做错了什么:(
更新:
首先,不要使用.toDate()
,否则会得到一个JavaScriptDate
对象,需要自己转换
其次,您需要在初始化的矩对象上调用.utcOffset()
。
moment(1473998400000).utcOffset(-240).format(); // 2016-09-16T00:00:00-04:00
moment(1473998400000).utcOffset(480).format(); // 2016-09-16T12:00:00+08:00
此外,请检查 docs 中的以下摘录:
If you want an actual time zone -- time in a particular location, like America/Los_Angeles, consider moment-timezone.