取消对某个时刻的 toDate 的区域

时间:2021-04-14 04:36:26

标签: moment-timezone

我的服务器是 UTC 时间。

我在洛杉矶创建了一个本地化的日期时间,如下所示:

var m = moment.tz('2021-04-13T20:30:53-07:00', 'America/Los_Angeles');

这部分效果很好。执行 m.date() 给了我 13。但是我需要将它作为 Date 对象而不是 Moment 提供给 lib,所以我要做的是:

var d = m.toDate()

但是现在我的服务器出错了。由于它在 UTC 中,因此它执行 d.getDate() 使其成为第 14 天,因为 UTC 提前 7 小时,因此此时间戳为凌晨 3:30。

有没有办法及时获得未分区的日期?这样即使在 UTC d.getDate() 也会给出第 13 位。

我编写了这个自定义函数,但我希望 moment-timezone 库提供了这个。

const getUnzonedDate = (m) => {
    return m.clone().add(m.utcOffset(), 'minutes').toDate();
}
另一个例子

如果我的系统时间是 UTC,上面的 getUnzoned 日期有效。假设我的系统时间在洛杉矶:

   * Removes zone information means, if system time is LA:
   * 
   * const m = moment.tz('2021-04-13T21:30:53', 'America/New_York');
   * m.hours() === m.toDate().getHours(); // false
   * m.hours() === getUnoznedDate(m).getHours(); // true
   * 
   * So it means that when the date will give you same numbers for each unit in
   * moment and as a date.

0 个答案:

没有答案
相关问题