簡體   English   中英

用時刻js解析格式化日期

[英]Parsing formatted date with moment js

我格式化傳入日期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))並沒有多大意義。 只需解析字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM