I am facing an issue while trying to calculate the time difference between two hours in my code snippet:
changEndHour($event: any, i: number) {
var arrayControl = this.generalForm.get('hours') as FormArray;
let startHour = arrayControl.at(i).get('startHour').value,
startMin = arrayControl.at(i).get('startMin').value,
endMin = arrayControl.at(i).get('endMin').value,
endHour = $event.split(":"), date1 = new Date(), date2 = new Date();
date1.setHours(startHour, startMin).toString();
let d1=date1.setHours(startHour, startMin).toString();
date2.setHours(endHour, endMin).toString();
let d2=date2.setHours(endHour, endMin).toString();
let hours = (Math.abs(date1.getTime() - date2.getTime()) / 1000 / 60 / 60).toFixed(2);
arrayControl.at(i).get('result').setValue(hours);
}
After testing the code with inputs 13:00 and 12:10, I found that it is yielding a result of 0.83 which is incorrect. I need assistance in improving this piece of code. Can someone help me solve this issue?