Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get days, hour and minutes diff moment js

I have two datetime pickers (from, to). I need to get difference between FROM and TO dates in minutes (1505 min) and in day and time (2 day 1h 35min).

I use moment.js

var now = moment('2018-03-28 14:02');
var end = moment('2018-06-02 00:00'); // another date
var duration = moment.duration(end.diff(now));
var days = duration.asDays();
console.log(days) //65.41527777777777

Output here is 65.41527777777777 where 65 is correct days, but how to convert 41527777777777 to hours and minutes. If I make this 0,41527777777777 * 24 = 9,96666666648 i get 9 hours, and again 0,96666666648 * 60 = 57 and this is correct difference

65 day, 9 hour and 57 min

But, is there any way to do this directly with moment.js?

Thank you

like image 662
Arter Avatar asked Mar 20 '18 13:03

Arter


Video Answer


1 Answers

Without a plugin:

var now = moment('2018-03-28 14:02');
var end = moment('2018-06-02 00:00'); // another date
var duration = moment.duration(end.diff(now));

//Get Days and subtract from duration
var days = duration.asDays();
duration.subtract(moment.duration(days,'days'));

//Get hours and subtract from duration
var hours = duration.hours();
duration.subtract(moment.duration(hours,'hours'));

//Get Minutes and subtract from duration
var minutes = duration.minutes();
duration.subtract(moment.duration(minutes,'minutes'));

//Get seconds
var seconds = duration.seconds();
console.log("Days: ",days);
console.log("Hours: ",hours);
console.log("Minutes: ",minutes);
console.log("Seconds: ",seconds);
like image 61
Matthew Mallimo Avatar answered Sep 29 '22 05:09

Matthew Mallimo