calculate business hours between two dates but limit each day to 8 hours
Hello I need to compute for business hours between two dates but would need to limit each day to 8 hours. Business Hours = 8am - 5pm Lunch Hour = 12pm - 1pm DateTimeStart = March 1 8am DateTimeEnd = March 2 9am Total business hours = 9 Has anyone encountered something like this? Have searched the help center but I didn't see anything there. I've managed to get the total hours to work but couldn't find a way to limit each day to 8 hours var d1 = moment(@Start Time); var d2 = moment(@End Time); var hours = d2.diff(d1, 'hours'); hours; Total hours = 25

  • Hi, this should work: var d1 = moment(@Start); var d2 = moment(@End); var hours = d2.diff(d1, 'hours'); var nwhrs = 16; var timeCorrect = parseFloat(moment(d2).tz("Europe/Berlin").format("ZZ"))/100; var lunchCheck = moment(d2).hours(12).subtract(timeCorrect, 'h'); var dayFactor = Math.floor(hours/24); var lunchFactor = d2 <= lunchCheck ? 0 : 1; hours-(nwhrs*dayFactor)-lunchFactor nwhrs = non work hours per day; timeCorrect could be necessary. You have to take your time zone (instead of my time zone 'Europe/Berlin'). If you get wrong results with timeCorrect just disable it. This formula includes the lunch break. So when your start = March 1 8am and end = March 2 14 pm the result should be 13 business hours, The formula is only usable for a workday with 8 business hours and a lunchbreak with 1 hour. Rainer rg@delos-consulting.com
This question is closed.