【问题标题】:moment js:month format number not starting with zero时刻 js:月份格式数不以零开头
【发布时间】:2018-01-17 05:05:39
【问题描述】:

我正在尝试将 UTC 时间戳格式化为日期字符串,并且我希望月份索引以 0 开头

例如:

0 - 一月,

1 - 二月

等等。

moment(1516102320000).format('YYYY-M-DD-h-mm-s');

输出: 2018-1-16-5-02-0

预期输出: 2018-0-16-5-02-0

根据他们的文档,月索引从零开始,但对我不起作用。

时刻版本:^2.20.1

【问题讨论】:

标签: javascript node.js momentjs


【解决方案1】:

format 方法将显示从 1 到 12 的月份。有关格式的详细信息,请参阅this 链接。输入为 0 索引。如果你特别需要0个索引月份,你可以使用moment.month

【讨论】:

【解决方案2】:

试试这个moment(1516102320000).year() +"-"+ moment(1516102320000).month()+"-"+moment(1516102320000).day()

这是实现所需输出的方式。 month() 将在一月份返回 0。 format() 将在 1 月份返回 1,因此为了实现此输出 2018-0-2 您必须使用此代码 moment(1516102320000).year() +"-"+ moment(1516102320000).month()+"-"+moment(1516102320000).day()

【讨论】:

  • 让我试试这个
  • 正如我提到的,您必须自己格式化。 month() 在 1 月返回 0,在 12 月返回 11。尝试答案中提到的完全相同的代码。让我知道它是否适合你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-02
  • 2017-11-17
相关资源
最近更新 更多