【发布时间】:2019-10-31 14:06:08
【问题描述】:
我有两个字符串代表Greenwich Mean Time (GMT) 中的日期/时间,我希望将它们转换为它们的Coordinated Universal Time (or UTC) 等效项。
-
2019-11-01 09:58 2019-10-01 08:58
对于第一个字符串,时间在转换为 UTC 时应该保持不变,因为在 11 月不使用 Daylight saving time (DST)。
而当转换为 UTC 时,第二个字符串的时间应该提前 1 小时,因为在 10 月初使用 DST。
我想用moment.js 和moment-timezone.js 转换这两个字符串
到目前为止,我尝试了以下但没有成功,有什么建议吗?
var format = 'D MMMM Y HH:mm';
console.log(
"First momement() call should return value with 09:58 and next one with 08:58': ",'\n',
moment("2019-11-01 09:58", 'YYYY-MM-DD HH:mm').tz('GMT').utc().format(format),'\n',
moment("2019-10-01 09:58", 'YYYY-MM-DD HH:mm').tz('GMT').utc().format(format)
);
console.log(
"First momement() call should return value with 09:58 and next one with 08:58': ",'\n',
moment.tz("2019-11-01 09:58", 'YYYY-MM-DD HH:mm',"GMT").utc().format(format),'\n',
moment.tz("2019-10-01 09:58", 'YYYY-MM-DD HH:mm',"GMT").utc().format(format)
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.5/moment-timezone-with-data-2010-2020.min.js"></script>
【问题讨论】:
-
There's no meaningful difference between the two。夏令时也没有改变:它们总是相同的时间。一个是时区,另一个是标准,但它们指的是同一个时钟时间。
标签: javascript momentjs datetime-conversion moment-timezone