【问题标题】:How to get current offset from UTC to Eastern Daylight Saving time using momentjs?如何使用 momentjs 获取从 UTC 到东部夏令时的当前偏移量?
【发布时间】:2017-07-24 14:01:43
【问题描述】:

我想知道现在(或任何时候)东部夏令时与 UTC 相比有多少秒不同。

例如,现在 UTC 与东部夏令时间相差 4 小时(提前)= 4*3600 = 14400 秒。 但在 12 月,这将变为提前 5 小时。

如何以编程方式编写该脚本,以便脚本知道每次使用 momentjs 运行时多少秒不同?

谢谢。

【问题讨论】:

标签: javascript node.js momentjs


【解决方案1】:

例如:

const moment = require('moment-timezone');

const zone = 'US/Eastern';
const utc = 'UTC';

const diff = date => moment.tz(date, zone)
  .diff(moment.tz(date, utc), 'hours');

['2017-02-01', '2017-05-01', '2017-08-01', '2017-11-01'].forEach(date => {
  console.log(`${date}: ${diff(date)} hours`);
});

输出:

2017-02-01: 5 hours
2017-05-01: 4 hours
2017-08-01: 4 hours
2017-11-01: 4 hours

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-27
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 2014-12-14
    • 2018-05-26
    相关资源
    最近更新 更多