端子輸出:爲什麼moment.js diff方法返回NaN?
Now: { _d: Sat Jan 13 2018 02:39:25 GMT-0400 (AST),
_isUTC: false,
_a: null,
_lang: false }
Expiration Date: { _d: Wed Feb 13 2013 02:00:15 GMT-0400 (AST),
_isUTC: false,
_a: null,
_lang: false }
Difference between Now and Expiration Date: NaN
代碼:
console.log('Difference between Now and Expiration Date:', now.diff(expDate, 'months', true));
moment.js來源:
diff : function (input, val, asFloat) {
var inputMoment = this._isUTC ? moment(input).utc() : moment(input).local(),
zoneDiff = (this.zone() - inputMoment.zone()) * 6e4,
diff = this._d - inputMoment._d - zoneDiff,
year = this.year() - inputMoment.year(),
month = this.month() - inputMoment.month(),
date = this.date() - inputMoment.date(),
output;
if (val === 'months') {
output = year * 12 + month + date/30;
} else if (val === 'years') {
output = year + (month + date/30)/12;
} else {
output = val === 'seconds' ? diff/1e3 : // 1000
val === 'minutes' ? diff/6e4 : // 1000 * 60
val === 'hours' ? diff/36e5 : // 1000 * 60 * 60
val === 'days' ? diff/864e5 : // 1000 * 60 * 60 * 24
val === 'weeks' ? diff/6048e5 : // 1000 * 60 * 60 * 24 * 7
diff;
}
return asFloat ? output : round(output);
}
您是否使用了最新的moment.js?我使用的是1.7.2,它讓我回到'59'。 – robertklep
是的,我正在使用1.7.2。我做了'時刻(expDate._d)',它工作。任何想法爲什麼? – crzrcn
是expDate一個真正的時刻() - 生成的對象?或者你從JSON反序列化的東西? – robertklep