如何区分两个小时之间的差异

时间:2017-11-10 07:11:18

标签: javascript jquery momentjs

我正在尝试使用momentjs

来区分两个小时之间的差异

但我无法获得hours count

这是我尝试过的,

var date1 = moment('2016-10-08 10:29:23');
var date2 = moment('2016-10-08 11:06:55');
var diff = date2.diff(date1);

console.log(diff);

问题:我希望获得1 day 4 hrs4 hrs 2 min等结果

1 个答案:

答案 0 :(得分:1)

基于您想要做的事情的几个选项:

var date1 = moment('2016-10-08 10:29:23');
var date2 = moment('2016-10-09 11:06:55');
var diff = date2.diff(date1);

var duration = moment.duration(diff);

console.log(duration.humanize(true));
// "in a day"

console.log(`${duration.days()}d ${duration.hours()}h ${duration.minutes()}m`);
// "1d 0h 37m"

console.log(duration.asHours());
// 24.625555555555554