Complexed Time Difference Formula

mahmed1

Well-known Member
Joined
Mar 28, 2009
Messages
2,302
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi All

I have column where i have a start date time and another column with End Date Time

What i want to do is calculate the difference but only the difference between my start shift excluding Sundays

Weekday 08:00 - 21:00
Saturday 09:00 - 15:00

So i need to look at the difference between the 2 values factoring in those shift pattern

eg

Start Date Time…..End Time…..Diff (expected)
02/10/2023 09:00….02/10/2023 10:00…01:00
02/10/2023 18:00….03/10/2022 10:00…04:00
20/10/2023 19:00…23/10/2023. 11:00.. 11:00

20/10 breakdown
Friday 02:00
Sat 06:00
Mon 03:00
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Maybe something like this will help you with your solution...
Doug
 
Upvote 0
Thank you - i can give this a go
The only thing in my case id need to factor in is that i need to exclude sundays and have another upper and lower limit on a Saturday

I dont know how to add that in :-(
 
Upvote 0
You may need to use the NETWORKDAYS.INTL for the weekend flexibility. I have not spent much time looking at it so I am not sure how the working hours calculations are done but I see them in the examples. Below is another link that may be helpful...
I also saw a link to this on the Exceljet site...
If you come up with a solution, please share as this may help others.

Doug
 
Upvote 0
Thank you

Ill try and see if Barrys Formula works
 
Upvote 0
Hi

I tried to get it to work but for some reason i get unexpected results

I followed Barrys steps but cant seen to get right hours

=SUMPRODUCT(INT((WEEKDAY(B6-W$2:W$8)+INT(C6)-INT(B6))/7),Z$2:Z$8)-SUMPRODUCT((J$2:J$10>B6)*(J$2:J$10< C6),LOOKUP(WEEKDAY(J$2:J$10),W$2:Z$8))+MOD(C6,1)-MOD(B6,1)+LOOKUP(WEEKDAY(B6),W$2:X$8)-LOOKUP(WEEKDAY(C6),W$2:Y$8)

I tried using the networkdays.itl function and that worked only for a set start and end time
 
Upvote 0
I got it part way there with NETWORKDAYS.INTL put in the formula from Exceljet. Admittedly, I have not figured out how that works yet:)

I only have it with the weekday hours and not sure how to implement the Saturday hours. I am out of time for looking at this right now...
Book1
ABCDEFGH
1StartEndDiffPart Way Thereupper21:00
22/10/2023 9:002/10/2023 10:0001:0001:00lower8:00
32/10/2023 18:003/10/2023 10:0004:0005:00
420/10/2023 19:0023/10/2023 11:0011:0018:00
Sheet2
Cell Formulas
RangeFormula
D2:D4D2=(NETWORKDAYS.INTL(A2,B2,11)-1)*(upper-lower)+IF(NETWORKDAYS.INTL(B2,B2,11),MEDIAN(MOD(B2,1),upper,lower),upper)-MEDIAN(NETWORKDAYS.INTL(A2,A2,11)*MOD(A2,1),upper,lower)
Named Ranges
NameRefers ToCells
lower=Sheet2!$H$2D2:D4
upper=Sheet2!$H$1D2:D4

Hope this helps,

Doug
 
Upvote 0
Thank you - hopefully someone can help to include Saturday hours too
 
Upvote 0
I have this working with the data you supplied. If the time difference exceeds 24 hours, it will return the number of hours over 24. For example, if result is 34 hours, it will show 10 hours. It probably does the same over 48 hours. I am tired and cannot think of a way to overcome this.
Book1
ABCDEFGHIJK
1StartEndDiffCalculatedDayWeekdayWorkdaystartend
22/10/2023 9:002/10/2023 10:0001:0001:00Sunday1FALSE09:0015:00
32/10/2023 18:003/10/2023 10:0005:0005:00Monday2TRUE08:0021:00
420/10/2023 19:0023/10/2023 11:0011:0011:00Tuesday3TRUE08:0021:00
5Wednesday4TRUE08:0021:00
6Thursday5TRUE08:0021:00
7Friday6TRUE08:0021:00
8Saturday7TRUE09:0015:00
Sheet1
Cell Formulas
RangeFormula
D2:D4D2=LET(start,A2,end,B2,sched,$H$2:$K$8,startday,ROUNDDOWN(start,0),endday,ROUNDDOWN(end,0),seq,SEQUENCE(endday-startday+1,,startday),wdays,WEEKDAY(seq),wrkdays,VLOOKUP(wdays,sched,2,0),starttimes,IFS(seq=MIN(seq),MOD(start,1),1,VLOOKUP(wdays,sched,3,0)),endtimes,IFS(seq=MAX(seq),MOD(end,1),1,VLOOKUP(wdays,sched,4,0)),SUM((endtimes-starttimes)*wrkdays))

You will have to create the work schedule table showing which days are workdays and the hours for each day.

Hope it works for you,

Doug
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Back
Top