Home > Net >  Calculate duration between start time and end time in typescript
Calculate duration between start time and end time in typescript

Time:12-15

I'm getting start time and end time using date pipe

 this.startTime=this.datePipe.transform(new Date(), 'hh:mm');

 this.endTime=this.datePipe.transform(new Date(), 'hh:mm');

output is like this : start time - 01:14 end time - 01:15

i want to calculate the difference between start time and end time, for current case it should return 1.

Any solution Thanks

CodePudding user response:

I assume it will have this date in form of strings, and you want to have difference in minutes:-

function findDiff(startTime, endTime) {
  const start = startTime.split(':');
  const end = endTime.split(':');

  let totalMinDifference = ((parseInt(end[0]) * 60)    parseInt(end[1])) - ((parseInt(start[0]) * 60)    parseInt(start[1]));

  return totalMinDifference ; // if want to have result difference in seconds multiply with 60
}

How to use :-

findDiff('01:14', '01:15')
  • Related