简体   繁体   English

用时刻js解析格式化日期

[英]Parsing formatted date with moment js

I'm formatting incoming date 2016-11-10T21:59:53.000+0000 with help moment js as: 我格式化传入日期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');
       };

the output result looks like: 10 Nov 2016 10:00 pm 输出结果如下: 10 Nov 2016 10:00 pm

Now I'm trying to parse this date back to string, but unfortunately my attempts are unsuccessful 现在我试图将这个日期解析回字符串,但不幸的是我的尝试都没有成功

  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

Could anybody explain me what I'm doing wrong? 谁能解释一下我做错了什么?

Thanks in advance. 提前致谢。

As you are parsing a non standard format, simply specify the format. 在解析非标准格式时,只需指定格式即可。 Then use moment's toDate() method to get a plain js Date object: 然后使用moment的toDate()方法获取一个普通的js Date对象:

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

SIDENODE SIDENODE

Using moment.utc(new Date(data.occuredDate)) doesn't really make sense. 使用moment.utc(new Date(data.occuredDate))并没有多大意义。 Just parse the string. 只需解析字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM