-5

I have just 2 string values like 02:00 PM and 10:00 PM. How to calculate the difference of those two.

I don't have date here. I only have time.

Krishna
  • 1,089
  • 5
  • 24
  • 38

1 Answers1

3

You can try this. I hope it's help you :

function diff(start, end) {
 start = start.split(":");
 end = end.split(":");
 var startDate = new Date(0, 0, 0, start[0], start[1], 0);
 var endDate = new Date(0, 0, 0, end[0], end[1], 0);
 var diff = endDate.getTime() - startDate.getTime();
 var hours = Math.floor(diff / 1000 / 60 / 60);
 diff -= hours * 1000 * 60 * 60;
 var minutes = Math.floor(diff / 1000 / 60);

// If using time pickers with 24 hours format, add the below line get exact hours
 if (hours < 0)
   hours = hours + 24;

 return (hours <= 9 ? "0" : "") + hours + ":" + (minutes <= 9 ? "0" : "") + minutes;
}
vibhor1997a
  • 2,336
  • 2
  • 17
  • 37
Manish Bohra
  • 340
  • 2
  • 11