Forum Discussion
calculate total hours between date/time (excluding weekends)
Hello,
I know that this is pretty easy for this group, but it's escaping me at the moment. I want to calculate the hours between two fields, but I just want a simple number of hours. No concatenated labels, not days, just accumulated hours and I need it to exclude weekends.
Example:
11/4/22 5:02 PM - 11/10/22 10:42 AM (minus weekends) = # of hours
NeilKloster So there are 2 things happening here:
a) any of the lines with start and end being the same day are 0 because you missed the last line of the equation
b) the numbers look weird because you didn't format them to show [h]:mm
If you want purely hours (i.e. [h]:mm of 1:30 would be 1.5) then just add 24* at the beginning (or *24 at the end) of the equation.
see attached.
- mtarlerSilver Contributor
I think this is same as another question:
https://techcommunity.microsoft.com/t5/excel/networkdays-formula/m-p/3693884
here is my answer there:
= NETWORKDAYS(C2,D2) - 1 + MOD(D2,1) - MOD(C2,1)
and format cells as [h]:mm:ss as you noted.
but again this doesn't take into account any work hours and doesn't account for any start or end dates that are on the weekend.
If the start or end might be a weekend then try:
= NETWORKDAYS(C2,D2) - NETWORKDAYS(C2,C2) - NETWORKDAYS(D2,D2) + 1 + MOD(D2,1) - MOD(C2,1)EDIT after thinking about this if it starts or end on a weekend then the MOD parts (the hours) shouldn't count either so:
= NETWORKDAYS(C2,D2) - NETWORKDAYS(C2,C2)*(1+MOD(C2,1)) - NETWORKDAYS(D2,D2)*(1- MOD(D2,1) ) + 1
- NeilKlosterBrass Contributor
Thank you for replying.
Sorry, I didn't even think to include start-end times.
lower / upper ranges for the start of the workday would be 7:00 AM and the end would be 11:00 PM.
- mtarlerSilver Contributorso should 2022-12-17 10:00PM to 2022-12-18 10:00AM be only 4 hours?
what about starting at 2022-12-17 11:30PM? would that be 3 hours?
- mtarlerSilver Contributor
NeilKloster so in order to account for every contingency the formula is large but hopefully understandable:
= LET(StartDay, C7, EndDay, D7, DayStart, TIME(7,0,0), DayEnd, TIME(23,0,0), incStart,NETWORKDAYS(StartDay, StartDay), incEnd,NETWORKDAYS(EndDay,EndDay), daysInBetween, NETWORKDAYS(StartDay,EndDay) - incStart - incEnd, hoursInBetween, daysInBetween*(DayEnd - DayStart), startHours, incStart*MAX(DayEnd - MAX(DayStart,MOD(StartDay,1)),0), endHours, incEnd*MAX(MIN(DayEnd,MOD(EndDay,1))-DayStart,0), IF(daysInBetween>=0, startHours + hoursInBetween + endHours, if(StartDay<EndDay, MIN(DayEnd,MOD(EndDay,1))-MAX(DayStart,MOD(StartDay,1)),0)))
so row 1 is the only thing you need to enter with the start day & end day, and then DayStart and DayEnd are the start and end of the work hours. After that:
lines 2&3: the incStart & incEnd check if the start/end days are weekdays or weekends,
line 4: daysInBetween finds how many valid workdays NOT counting the 1st or last,
line 5: converts full days to # of work hours,
line 6&7: calculate the # hours on the 1st and last days
line 8: checks if # daysInBetween is valid (i.e. Start Day is > End Day)
line 9: adds hours together in 'normal' cases
line 10: calculates # hours if start Day is same as end Day or gives 0 if start is AFTER end
- NeilKlosterBrass Contributor
Okay, first and foremost - you are awesome! The amount of effort you are putting into this to help me is amazing! TYSM!!
So I put in that amazing formula and I'm getting results, but they are off. I am including a simplified file example to show you what is going on. Please ignore the conditional format coloring on column C.
- mtarlerSilver Contributor
NeilKloster So there are 2 things happening here:
a) any of the lines with start and end being the same day are 0 because you missed the last line of the equation
b) the numbers look weird because you didn't format them to show [h]:mm
If you want purely hours (i.e. [h]:mm of 1:30 would be 1.5) then just add 24* at the beginning (or *24 at the end) of the equation.
see attached.