【问题标题】:Convert these strings of GMT time/date to UTC time/date with moment.js使用 moment.js 将这些 GMT 时间/日期字符串转换为 UTC 时间/日期
【发布时间】:2019-10-31 14:06:08
【问题描述】:

我有两个字符串代表Greenwich Mean Time (GMT) 中的日期/时间,我希望将它们转换为它们的Coordinated Universal Time (or UTC) 等效项。

  1. 2019-11-01 09:58
  2. 2019-10-01 08:58

对于第一个字符串,时间在转换为 UTC 时应该保持不变,因为在 11 月不使用 Daylight saving time (DST)

而当转换为 UTC 时,第二个字符串的时间应该提前 1 小时,因为在 10 月初使用 DST。

我想用moment.jsmoment-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>

【问题讨论】:

标签: javascript momentjs datetime-conversion moment-timezone


【解决方案1】:

啊,我知道是什么了。我需要使用时区位置/地点,而不是使用GMT 来转换它。

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.tz("2019-11-01 09:58", 'YYYY-MM-DD HH:mm','Europe/London').utc().format(format),'\n',
  moment.tz("2019-10-01 09:58", 'YYYY-MM-DD HH:mm','Europe/London').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>

【讨论】:

    猜你喜欢
    • 2011-06-13
    • 1970-01-01
    • 2011-10-07
    • 2015-10-24
    • 1970-01-01
    • 2014-02-22
    • 2014-11-18
    • 1970-01-01
    • 2012-03-15
    相关资源
    最近更新 更多