Forum Discussion
calculate total hours between date/time (excluding weekends)
- Dec 29, 2022
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.
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
- NeilKlosterDec 29, 2022Brass 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.
- HansVogelaarDec 29, 2022MVP
A shorter formula that works if neither Date/Time Opened not Date/Time Closed will be a weekend day:
=IF(OR(A2:B2=""),"",16*(NETWORKDAYS(A2,B2)-1)+24*(MOD(B2,1)-MOD(A2,1)))
- NeilKlosterDec 30, 2022Brass ContributorHaha!! This is also works EXACTLY like the other one too - I just checked them both. Thank you so much!! Much appreciated everyone!
- mtarlerDec 29, 2022Silver 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.
- NeilKlosterJan 13, 2023Brass Contributor
So now the client has come back and asked for holidays to also be excluded. I know that there is a way to do this, by creating a list of dates, but when I tried it, it didn't work for me. Any thoughts on how to include this as well?