簡體   English   中英

為什么moment.js返回此日期錯誤?

[英]Why does moment.js return this date wrong?

我只是想獲取一個輸入字符串並將其轉換為日期對象。

moment.utc('2000-01-01T00:00:00.000Z').toDate()

但是它返回了...

Fri Dec 31 1999 19:00:00 GMT-0500 (EST)

謝謝

那是一個有效的JavaScript Date對象。 您可以通過在Chrome或Firefox中打開控制台,然后輸入以下內容來進行測試:

// Mon Nov 24 2014 09:54:00 GMT-0800 (PST) (looks the same as your example)
console.log( new Date() );

如果要格式化來自moment.js的值,可以使用其格式化方法和掩碼。

// Example: November 24th 2014, 09:58:12am
var fdate = moment().format('MMMM Do YYYY, h:mm:ss a');

Moment.js不會修改原型,而只是包裝它。

如果您想使用moment.js將字符串轉換為日期對象,則可以這樣調用它:

moment(your_date); // Unless in UTC mode this will display as local time

在您的實例中,您使用的是UTC模式

2000-01-01T00:00:00.000Z是GMT日期/時間。

使用moment.utc("2000-01-01T00:00:00.000Z").toDate()根據您的時區設置返回此日期/時間。

參見: http : //www.digitoffee.com/programming/get-local-time-utc-using-moment-js/94/

希望能幫助到你。

暫無
暫無

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

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