【问题标题】:moment.js - Get day from any local date not in UTCmoment.js - 从任何非 UTC 的本地日期获取日期
【发布时间】:2019-10-23 16:15:03
【问题描述】:

我想获得一个独立于时区偏移的日期

const dateOne = moment('Thu Oct 20 2019 12:00:00 GMT-1200').format('YYYY/MM/DD'); // 2019/10/21
const dateTwo = moment('Thu Oct 20 2019 12:00:00 GMT+2300').format('YYYY/MM/DD'); // 2019/10/19

但是对于这两种情况,我都需要得到 2019/10/20。 JS 日期和时刻也在格式化受 GMT 偏移影响的日期。如何避免?

【问题讨论】:

  • 您可以将您的时区偏移量添加到日期的 valueOf() 中,以使 UTC 看起来是本地的。
  • parseZone 实际上对我有用
  • GMT+2300 在哪里?
  • 如果您不想要时间或时区,请修剪它们:moment('Thu Oct 20 2019 12:00:00 GMT-1200'.slice(0,12)).format('YYYY/MM/DD')

标签: javascript date momentjs


【解决方案1】:

moment.parseZone 方法对我有用

const date1 = moment.parseZone('Thu Oct 20 2019 12:00:00 GMT-1200').format('YYYY-MM-DD');
const date2 = moment.parseZone('Thu Oct 20 2019 12:00:00 GMT+2300').format('YYYY-MM-DD');
console.log(date1 === date2); // true

【讨论】:

    猜你喜欢
    • 2015-05-07
    • 2013-10-24
    • 1970-01-01
    • 2021-03-26
    • 2014-11-11
    • 2015-02-24
    • 2016-11-14
    • 2021-04-13
    • 1970-01-01
    相关资源
    最近更新 更多