Working hours between two date/times excluding Lunch and weekends, diff time on Friday

Taul

Well-known Member
Hi,
I need some assistance with calculating working hours between two date/time cells. The working hours are 8:30 to 17:30 and the lunch break is 12:30 to 1:45

The date/time value could be outside the working hours but I only want to count the period within the working hours.

I have an existing formula (from Barry Houdini) that calculates the working hours minus the lunch breaks and excludes weekends and it works with date times outside the working hours.

The formula in cell M2 is:-
Code:
``=(NETWORKDAYS(F2,G2,holiday)-1)*(EndTime-StartTime+StartLun-EndLun)+IF(NETWORKDAYS(G2,G2,holiday),MEDIAN(MOD(G2,1), EndTime,StartTime)-MEDIAN(MOD(G2,1), EndLun,StartLun),EndTime-EndLun)-IF(NETWORKDAYS(F2,F2,holiday),MEDIAN(MOD(F2,1), EndTime,StartTime)-MEDIAN(MOD(F2,1), EndLun,StartLun),StartTime-StartLun)``

The date/time stamps are in cells:-
Start Date F2
End Date G2

I have Named Ranges for the date times and holidays used for the standard working week:-
StartTime
StartLun
EndLun
EndTime
Holiday (Range of 15 cells)

BUT … the working hours are fixed for Monday to Friday.
I would like to incorporate different working hours for Friday (8:30 to 15:00) with the same lunch break as Monday to Thursday. Is this possible?

Would anyone be able to assist in modifying the existing formula or perhaps come up with a different approach that would allow different working hours on a Friday?

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
 col H row 5 15/01/2019 08:30 17:00 7.25 7.25 16/01/2019 08:30 17:00 7.25 14.5 17/01/2019 08:30 17:00 7.25 21.75 18/01/2019 08:30 17:00 7.25 29 19/01/2019 08:30 15:00 5.25 34.25 22/01/2019 08:30 17:00 7.25 41.5 23/01/2019 08:30 17:00 7.25 48.75 24/01/2019 08:30 17:00 7.25 56 25/01/2019 08:30 17:00 7.25 63.25 26/01/2019 08:30 15:00 5.25 68.5 29/01/2019 08:30 17:00 7.25 75.75 30/01/2019 08:30 17:00 7.25 83 31/01/2019 08:30 17:00 7.25 90.25 01/02/2019 08:30 17:00 7.25 97.5 02/02/2019 08:30 15:00 5.25 102.75 05/02/2019 08:30 17:00 7.25 110 06/02/2019 08:30 17:00 7.25 117.25 07/02/2019 08:30 17:00 7.25 124.5 08/02/2019 08:30 17:00 7.25 131.75 09/02/2019 08:30 15:00 5.25 137 col I working hours between 17/01/2019 row 33 and 07/02/2019 row 34 110.00 formula =OFFSET(\$H\$4,MATCH(\$I\$34,\$H\$5:\$H\$24,0),4)-OFFSET(\$H\$4,MATCH(\$I\$33,\$H\$5:\$H\$24,0)-1,4)

<colgroup><col><col><col span="5"><col><col><col span="6"></colgroup><tbody>
</tbody>

Hi Oldbrewer,
Thank you for the reply, it looks like you have spent some time on this one.
Your formula accounts for different hours on a Friday but it does not account for lunch breaks or holidays. Also, it would (I think) require a very long list of dates from which to reference the formula.
Paul.

the lunch break is already built in, check the working hours each day.........sat and sun are also accounted for....an employees holidays would need to be logged in another table and any "hours of holiday" would be deducted.......the dates them selves are generated from a formula - I could take it to 2020 in 5 seconds

Thank you Oldbrewer,
I did not really grasp the full solution you offered, I think my brain must have been worn out as I spent the last few days looking for a work around to the problem. You are right, I could auto-generate the date list.

But there is still a problem, the cells containing the dates would be “date & time”.

So if the period I wanted to measure was quite short like:-
From [ 7/1/19 08:00] to [7/1/19 11:00] the formula would still deduct a lunch break for that period of time.

Your formula would work very well if the period of time crossed several whole days which included the lunch period.
I will keep playing with it and see if it can be adapted but I think this is a really difficult one to solve.

you build in =if(and(t2-t1<18,t2<12:30),0,1.25)

you get the idea ?

and what if they clock off during the lunch break ?

Hi Oldbrewer,
Just to explain a little more, I’m trying to create a generic formula to handle working periods of time that would be better than the existing formula that I mentioned in the original post. The only flaw (that I can see) with the original formula is it cannot handle a shorter working day on a Friday.

The formula is used in a variety of workbooks for different uses, some workbooks measure absenteeism, and some measure holiday periods, some measure project working periods for KPI measurement and will probably be used for other projects in the future where time measurement is needed.
So I'm trying to imagine if someone is likely to log-off during a break time, probably not.

I will use your suggestions later today (RL is getting in the way at the moment) and see what I can come up with.
Many thanks
Paul.

the original formula is too complicated for me, sorry.

Hi,
Just for the sake of completeness and in case anyone else is looking for a similar solution. I have found an alternative approach. So I thought I would share it.

https://pedrowave.blogspot.com/2013/03/hours-between-dates.html
The basic formula uses a range:- 0000111, seven numbers to represent the days of the week.
1= not active
0 = Active
So I have use three ranges
Range1 = Mon to Thursday (0000111)
Range2 = Fri (1111011)
Range3 = Lunch Break, Mon to Fri (0000011)

Each range is calculated in a separate column and then sum(Range1 + Range2 – Range3)
Holidays are in a separate named range and it works for date times within or outside of the working hours but only measures the time spent within the working hours.

Cheers
Paul.

Replies
2
Views
205
Replies
3
Views
127
Replies
1
Views
302
Replies
3
Views
823
Replies
1
Views
899

1,207,012
Messages
6,076,148
Members
446,187
Latest member
LMill

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back