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?
diff(startTime)); var hours = duration. hours(); it is returning the current hour and not the number of hours between the two dates.
Calculate the time difference in hours and minutes with: split(":"); var time1 = splitted1[0]+splitted1[1]; var time2 = splitted2[0]+splitted2[1]; var hours; var minutes; if (time1 < time2) { var diff = getTimeDiff('{time2}', '{time1}', 'm'); hours = Math.
The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().
You were close. You just need to use the duration.asHours()
method (see the docs).
var duration = moment.duration(end.diff(startTime)); var hours = duration.asHours();
Following code block shows how to calculate the difference in number of days between two dates using MomentJS.
var now = moment(new Date()); //todays date var end = moment("2015-12-1"); // another date var duration = moment.duration(now.diff(end)); var days = duration.asDays(); console.log(days)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With