Having a difficult time with this, let me present you with a scenario:
A waiter at a restaurant earns $15/hour, but between 9:00 PM and 2:30 AM, he gets paid an additional $3/hour. I have the 'start' and 'end' of the shift as Date objects, and I want to calculate how many hours of that extra paid period are within the shift, if any. The start and end times of the shift can vary, which makes it challenging.
I managed to make it work using a lot of if statements, but I know that's not the most efficient way. Here is the code snippet link, but it's quite messy (involving two different time periods with extra pay). Does anyone have a better idea of how to calculate this? Are there any classes or methods that could assist in this calculation?
static calculateShiftSalary(shift: Shift, employee: Employee): Shift {
const extraPaidStart = 22
const extraPaidEnd= 2.5 // meaning 02:30 o'clock -of the next day here
let start:Date = shift.startDate
let end:Date = shift.endDate
let extraHours:number =
getNumberOfExtraPaidHours(start,end,extraPaidStart,extraPaidEnd)
}
So, for example, if the shift is between 8:00 PM and 11:30 PM, 'extraHours' would be 1.5. If the shift is between 11:00 PM and 2:00 AM, 'extraHours' would be 3 hours.
Feel free to change 'extraPaidStart' and 'extraPaidEnd' to Date objects if that would be more beneficial.