I'm able to get the difference between two dates using MomentJs as follows:
moment(end.diff(startTime)).format("m[m] s[s]")
However, I also want to display the hour when applicable (only when >= 60 minutes have passed).
However, when I try to retrieve the duration hours using the following:
var duration = moment.duration(end.diff(startTime));
var hours = duration.hours();
it is returning the current hour and not the number of hours between the two dates.
How do I get the difference in hours between two Moments?
You were close. You just need to use the
duration.asHours()
method (see the docs).All you need to do is pass in
hours
as the second parameter to moments diff function.Docs: https://momentjs.com/docs/#/displaying/difference/
Example:
Or you can do simply:
docs: here
As you can see, the start and end date needed to be moment objects for this method to work.
Following code block shows how to calculate the difference in number of days between two dates using MomentJS.