Search code examples
momentjsangular-moment

Wrong time difference with momentjs diff function


i'm trying to calculate the difference between two UTC Datetime Strings with angular-momentjs like shown below:

var start = "1970-01-01T11:03:00.000Z";
var end = "1970-01-01T11:15:00.000Z";

var duration = $moment.utc($moment(end).diff($moment(start))).format("hh:mm");

when i execute the code above, the duration should be 00:12 but actually it is 12:12. I don't understand why and how to fix it.


Solution

  • You are actually creating a moment.js object for 1970-01-01T00:12:00.000Z, then getting the time as hour and minutes. The token "hh" is for 12 hour time, so you're seeing "12" for 12am. If you want to see 00:12, use the token "HH" which gives 24 hour time: 00:12.