用moment js解析格式化日期

Parsing formatted date with moment js

本文关键字:格式化 日期 moment js      更新时间:2023-09-26

我正在格式化输入日期2016-11-10T21:59:53.000+0000与帮助时刻js为:

  myService.getDate(id)
       .then(function (data) {
           data.occuredDate = moment.utc(new Date(data.occuredDate)).format('DD MMM YYYY h:mm a');
       };

输出结果如下:10 Nov 2016 10:00 pm

现在我试图解析这个日期回到字符串,但不幸的是我的尝试是不成功的

  console.log(new Date(obj.occuredDate))
  console.log(new Date(obj.occuredDate).toString())
  console.log(Date.parse(obj.occuredDate))
  console.log(new Date(Date.parse(obj.occuredDate)))
  console.log(new Date(Date.parse(obj.occuredDate)).toString())
  Invalid Date
  Invalid Date
  NaN
  Invalid Date
  Invalid Date
谁能告诉我我做错了什么?

提前感谢。

在解析非标准格式时,只需指定格式即可。然后使用moment的toDate()方法获得一个普通的js Date对象:

moment.utc(obj.occuredDate, 'DD MMM YYYY h:mm a').toDate();

SIDENODE

使用moment.utc(new Date(data.occuredDate))实际上没有意义。解析字符串