簡體   English   中英

通過moment.js轉換后,我得到了錯誤的日期

[英]I am getting wrong date after converting by moment.js

我的代碼是

console.log(moment(new Date('2016-05-24T18:50:05.000')).format('LL'));

應該是2016年5月24日,但它給了我2016年5月25日

誰能幫我。

您需要將日期指定為UTC。

您可以通過以下兩種方式之一進行操作:

console.log(moment(new Date('2016-05-24T18:50:05.000Z')).format('LL'));

其中,Z指定時間為UTC。 要么:

console.log(moment.utc(new Date('2016-05-24T18:50:05.000')).format('LL'));


您可以直接將ISO 8601日期字符串傳遞到時刻。

無需將字符串包裝在javascript Date對象中。 然后您的代碼變為:

console.log(moment('2016-05-24T18:50:05.000Z').format('LL'));

要么:

console.log(moment.utc('2016-05-24T18:50:05.000').format('LL'));

暫無
暫無

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

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