让我们说,我有一个两个日期时刻日期时间对象。
momentdatefirst = '2017-03-21T05:00:00+05:45'
momentdatesecond = '1990-03-21T07:12:45+05:45'
如何计算moment.js中这两个时间戳之间的差异?
我试过了:
var diff_hour = momentdatefirst.diff(momentdatesecond);
这并不能找回实际的差异。
答案 0 :(得分:2)
你应该这样做:
let date = moment('2017-03-21T05:00:00+05:45');
let dateTwo = moment('1990-03-21T07:12:45+05:45');
let diff = dateTwo.diff(date); // in milliseconds
let diffInHours = date.diff(dateTwo, 'hour'); // in hours
// and so on
console.log('diff in milliseconds', diff);
输出852068835000
毫秒〜27年。
答案 1 :(得分:2)
var a = moment('2016-06-06T21:03:55');//now
var b = moment('2016-05-06T20:03:55');
console.log(a.diff(b, 'minutes')) // 44700
console.log(a.diff(b, 'hours')) // 745
console.log(a.diff(b, 'days')) // 31
console.log(a.diff(b, 'weeks')) // 4
您可以浏览一下文档here