Goutham Ggs
Goutham Ggs

Reputation: 91

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

Upvotes: 0

Views: 72

Answers (1)

klugjo
klugjo

Reputation: 20885

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

Upvotes: 1

Related Questions