Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the time difference in hh:mm between two datetimes using moment

Tags:

momentjs

I am having issue with getting the time difference between two timestamps (epoch format).

I used moment().format("L LT") to convert it into desired datetime format (MM/DD/YYYY HH:MM (12 hour)), but now the problem is, I want the difference between the times in hh:mm

P.S.: it can be more than 24 hours too.

What I tried:

let timeDiffms,duration,timedifference,
start moment(startTimeStampInMS).format("L LT"),
end = moment(endTimeStampInMS).format("L LT");

timeDiffms = moment(end, "MM/DD/YYYY HH:mm").diff(moment(start, "MM/DD/YYYY HH:mm"));
duration = moment.duration(timeDiffms);
timedifference = Math.floor(duration.asHours()) +":"+duration.minutes();  // result

//output I am getting  
// start = "03/20/2017 3:11 PM" end="03/21/2017 9:45 AM" timedifference = "30:24", (incorrect)
// start = "03/20/2017 10:07 AM" end="03/23/2017 11:24 AM" timedifference = "73:17" (correct)
// start = "03/20/2017 3:11 PM" end="03/23/2017 11:31 AM" timedifference = "80:20" (incorrect) strange 

I don't know what is happening wrong here.

like image 716
prabh_singh Avatar asked Feb 04 '23 19:02

prabh_singh


2 Answers

Based on the previous answer given, I was able to workaround on same logic and get the correct solution for my question.

let start moment(startTimeStampInMS),
end = moment(endTimeStampInMS);

let timediffms = end-start;    // difference in ms
let duration = moment.duration(timediffms);
 // _data should not be used because it is an internal property and can 
 //change anytime without  any warning 
//let hours = duration._data.days*24 + duration._data.hours; 
//let min = duration._data.minutes;
let hours = duration.days()*24+ duration.hours();
let min = duration.minutes();
timeDifference = hours +":"+ min;  // Answer
like image 50
prabh_singh Avatar answered Apr 27 '23 03:04

prabh_singh


In your current solution you are formatting a moment object as string and then parse the string back to moment to get diff. There is no need to do this two-step process, you can do the following to get the duration:

start moment(startTimeStampInMS),
end = moment(endTimeStampInMS).format("L LT");

timeDiffms = end.diff(start);
duration = moment.duration(timeDiffms);

Or since your input is in milliseconds:

moment.duration(endTimeStampInMS - startTimeStampInMS);

To get duration in the HH:mm format you can use moment-duration-format plug-in that adds the format method to duration object.

Here a working sample:

// Example milliseconds input
let startTimeStampInMS = 1490019060000;
let endTimeStampInMS = 1490085900000;
// Build moment duration object
let duration = moment.duration(endTimeStampInMS - startTimeStampInMS);
// Format duration in HH:mm format
console.log( duration.format('HH:mm', {trim: false}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>
like image 27
VincenzoC Avatar answered Apr 27 '23 04:04

VincenzoC