2016-11-29 22 views
0

我的控制檯tomorow昨天給:Moment.js反向

enter image description here

相同的W/O新日期的構造:

enter image description here

什麼可能是錯誤的?

PS:因爲我對momentjs.com越來越控制檯相同的結果是不是與項目相關的

編輯: 要容易複製,去momentjs.com,打開控制檯,放:

moment().calendar('2016-11-27T07:31:29.000+0000'); // expecting "Sunday at 9:58 AM" getting "Tuesday at 9:58 AM" 
moment().calendar('2016-11-28T07:31:29.000+0000'); // expecting "Yesterday at 9:58 AM" getting "Tomorrow at 9:58 AM" 
moment().calendar('2016-11-29T07:31:29.000+0000'); // this is correct "Today at 9:58 AM" 
moment().calendar('2016-11-30T07:31:29.000+0000'); // expecting "Tomorrow at 9:58 AM" getting "Yesterday at 9:58 AM" 
+0

你能檢查一下新日期(...)的分析嗎?那就是''console.log(new Date(...))''。我認爲Moment.JS解決的問題之一是日期解析在這裏和那裏表現得很尷尬。所以,我會用Moments自己的日期解析器來開始。 – tiblu

+0

也許像這樣'moment('2016-11-28T07:31:29.000 + 0000')。calendar()'? – Andrey

+0

@Andrey同樣的結果: moment('2016-11-28T07:31:29.000 + 0000')。calendar() 「昨天早上8:31」 – Luckylooke

回答

2

docs它說:

moment().calendar(referenceTime); 

日曆時間顯示相對於給定referenceTime時間(默認爲現在),但確實因此您的字符串時間是參考,所以您得到的結果是正確的,因爲您的字符串時間是參考。

對於正確的結果使用:

moment('2016-11-30T07:31:29.000+0000').calendar(); 
0

.calendar()的第二個參數是參考時間。 Docs Here

如果您正在嘗試更改某物的日期。相反,創建它想:

moment().subtract(1, 'days').calendar() 

moment('2016-11-28T07:31:29.000+0000').calendar() 
0

因此,作爲@Andrey指出正確的順序是:

moment('2016-11-28T07:31:29.000+0000').calendar() 

moment().calendar('2016-11-28T07:31:29.000+0000') 

謝謝@Andrey