简体   繁体   中英

How to get the difference between 2 dates in hours

i'm trying to get the difference between 2 dates in hours using momentjs ,

but i'm unable to get hours count .

here is what i have tried,

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);

Question:i want t get result like 1 day 4 hrs , 4 hrs 2 min etc

A few options based on what you want to do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM