function workingHoursBetweenTimes(startDate, endDate) {
var minutesWorked = 0;
var minutesOT = 0;
if (endDate <= startDate) {
return 0;
}
while (startDate < endDate) {
var currentTime = startDate.getHours() + (startDate.getMinutes() / 60);
if (currentTime >= 8.5 && currentTime < 17.5 && (currentTime < 12 || currentTime >= 13)) {
minutesWorked++;
}
if (currentTime >= 17.75) {
minutesOT++;
}
startDate.setTime(startDate.getTime() + 1000 * 60);
}
return { worked: Math.round(minutesWorked / 60 * 100) / 100, ot: Math.round(minutesOT / 60 * 100) / 100 };
}
var times = workingHoursBetweenTimes(new Date("01/01/2022 " + "13:00"), new Date("01/01/2022 " + "18:00"));
console.log(times);