【问题标题】:momentjs does not return the correct date formatmomentjs 没有返回正确的日期格式
【发布时间】:2020-11-15 09:55:48
【问题描述】:

我指的是 nodejs 的 momentjs 库,我的代码如下所示

console.log("For date:" + item.DueDate + " the format is:" + moment(item.DueDate).format("do, MMM"))

上面的输出是

For UTC date:2020-07-28T00:00:00.000+0000 the format is:2nd, Jul 

我很困惑为什么将 7 月 28 日打印为 2 月 2 日?

我也试过了:

moment(item.DueDate).utc().format("do, MMM")

但结果相同。

【问题讨论】:

    标签: node.js date datetime momentjs


    【解决方案1】:

    您使用的是do星期,带有序数)而不是Do月份,带有序数)。

    引用docs

    Day of Month  D       1 2 ... 30 31
                  Do      1st 2nd ... 30th 31st <<<<<<<< THIS IS WHAT YOU WANTED
                  DD      01 02 ... 30 31
    [...]
    Day of Week   d       0 1 ... 5 6
                  do      0th 1st ... 5th 6th <<<<<<<<<< THIS IS WHAT YOU USED INSTEAD
                  dd      Su Mo ... Fr Sa
                  ddd     Sun Mon ... Fri Sat
                  dddd    Sunday Monday ... Friday Saturday
    

    因此,请改用moment(item.DueDate).format("Do, MMM")

    【讨论】:

    • 让我看看这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 2013-12-07
    • 2011-09-27
    • 1970-01-01
    • 2020-01-06
    相关资源
    最近更新 更多