Issue with finding time difference taking account of weekends and workdays.

BigGee

New Member
Joined
Sep 8, 2020
Messages
31
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello,
I am having trouble calculating a time difference between 2 x calculated dates taking into account workdays / weekends and working hours. Could someone help me coming up with a working formula?
I have tried NETWORKDAYS - but don't know how to account for working hours.

My problem is that I have the following working days
Mon-Thu - 6.30 - 3.30pm with 15min break at 9.30 and 30 min break at 12.30
Fri - 6.30-11.45 with 15min break at 9.30

Would appreciate any help

Thanks

1726491830888.png
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Would this work?:

I also consideres if time difference in negative. If that is never going to happen and if you need the time difference to be a number and not a text, then that is an easy fix.

U2:U3 is formatted as a table named T_Holidays where you can add all your holidays.
result in column G, H, I is formatted as "[h]:mm".

Book1.xlsx
ABCDEFGHIJKLMNOPQRSTU
1Online DateTarget Offline DateActual OfflineOnline To Target [h:mm]Online to Actual [h:mm]Difference [h:mm]
211/09/2024 07:3113/09/2024 14:5916/09/2024 08:3420:2922:332:04DaysShift StartShift endBreak Start 1Break end 1Break Start 2Break end 2Holidays
311/09/2024 09:1716/09/2024 10:0016/09/2024 10:4521:5822:430:45Mon-Thu06:3015:3009:3009:4512:3013:0017/09/2024
411/09/2024 11:1816/09/2024 11:4616/09/2024 11:4621:5821:580:00Fri06:3011:4509:3009:45
516/09/2024 15:2518/09/2024 06:3516/09/2024 15:300:100:05- 0:05
616/09/2024 15:2518/09/2024 06:3518/09/2024 06:550:100:300:20
716/09/2024 06:0016/09/2024 16:008:15
816/09/2024 06:0017/09/2024 16:008:15
916/09/2024 06:0018/09/2024 16:0016:30
1016/09/2024 06:0016/09/2024 16:00 8:15
1116/09/2024 06:0017/09/2024 16:00 8:15
1216/09/2024 06:0018/09/2024 16:00 16:30
1316/09/2024 09:2516/09/2024 09:5016/09/2024 09:510:100:110:01
1416/09/2024 12:2516/09/2024 13:0516/09/2024 13:060:100:110:01
Sheet1
Cell Formulas
RangeFormula
G2:I14G2=LET( timeBetween, LAMBDA(t_1,t_2, LET(n, ROUND((t_2-t_1)*24*60, 0), s, SEQUENCE(n,,t_1, TIME(0,1,0)), d, INT(s), m, VALUE(TEXT(s, "hh:mm")), isWorkingDay, WORKDAY.INTL(d-1, 1, 1, T_Holidays)=d, isWorkingHour, ((WEEKDAY(d, 2)<5)*(m>= $M$3)*(m<$N$3)*NOT((m>=$O$3)*(m<$P$3)+(m>=$Q$3)*(m<$R$3))+(WEEKDAY(d, 2)=5)*(m>= $M$4)*(m<$N$4)*NOT((m>=$O$4)*(m<$P$4)))>0, SUM(isWorkingDay*isWorkingHour)/24/60 )), onlineToTargetOffline, timeBetween(D2, E2), onlineToActualOffline, timeBetween(D2, F2), dif, onlineToActualOffline-onlineToTargetOffline, HSTACK(IFERROR(onlineToTargetOffline, ""), IFERROR(onlineToActualOffline, ""), IFERROR(IF(dif<0, "- ", "") & TEXT(ABS(dif), "[h]:mm"), "")) )
Dynamic array formulas.


Let me know if this works for you.
 
Upvote 0
Solution
1726640244774.png

Formula in H3 and copied down
Excel Formula:
=TimeDifference($E3,$F3,$K$2,$K$3,$K$4,$L$2,$L$3,$M$2,$M$3)
VBA code for UDF(User Defined Function) is used.
Code:
Option Explicit


VBA Code:
Function TimeDifference(StartDt As Range, EndDt As Range, ShiftStart As Range, ShiftEnd1 As Range, ShiftEnd2 As Range, BrkStart1 As Range, BrkEnd1 As Range, _
                        BrkStart2 As Range, BrkEnd2 As Range)
Dim st#, nd#, stb1#, stb2#, ndb1#, ndb2#, Shst#, Shnd1#, shnd2#, stmd#, ndmd#, Shstmd#, shnd#
Dim totdiff#, diff1#, Days&, T&
Dim wf As WorksheetFunction
st = StartDt: nd = EndDt: stb1 = BrkStart1: stb2 = BrkStart2: ndb1 = BrkEnd1: ndb2 = BrkEnd2
Shst = ShiftStart: Shnd1 = ShiftEnd1: shnd2 = ShiftEnd1
stmd = st - Int(st): ndmd = nd - Int(nd)
Shstmd = Shst - Int(Shst)
totdiff = 0
Days = Int(nd) - Int(st) + 1

If Days = 1 Then
If WorksheetFunction.Weekday(Int(st), 2) < 5 Then shnd = Shnd1 Else shnd = shnd2
diff1 = WorksheetFunction.Median(shnd, Shst, ndmd) - WorksheetFunction.Median(shnd, Shst, stmd)
If stmd <= stb1 And ndmd >= ndb1 Then
totdiff = diff1 - TimeValue("00:30:00")
Else
totdiff = diff1
End If
If stmd <= stb2 And ndmd >= ndb2 And WorksheetFunction.Weekday(Int(st), 2) < 5 Then
totdiff = totdiff - TimeValue("00:30:00")
End If

End If

diff1 = 0

If Days >= 2 Then
If WorksheetFunction.Weekday(Int(st), 2) < 5 Then shnd = Shnd1 Else shnd = shnd2
diff1 = shnd - WorksheetFunction.Median(shnd, Shst, stmd)
If stmd <= stb1 Then
totdiff = diff1 - TimeValue("00:30:00")
Else
totdiff = diff1
End If
'K = WorksheetFunction.Weekday(45547, 2)
If stmd <= stb2 And WorksheetFunction.Weekday(Int(st), 2) < 5 Then
totdiff = totdiff - TimeValue("00:30:00")
End If
diff1 = 0

If WorksheetFunction.Weekday(Int(nd), 2) < 5 Then shnd = Shnd1 Else shnd = shnd2
diff1 = WorksheetFunction.Median(shnd, Shst, ndmd) - Shst
If ndmd >= stb1 Then
totdiff = totdiff + diff1 - TimeValue("00:30:00")
Else
totdiff = totdiff + diff1
End If
If ndmd >= stb2 And WorksheetFunction.Weekday(Int(nd), 2) < 5 Then
totdiff = totdiff - TimeValue("00:30:00")
End If

End If

If Days > 2 Then
For T = Int(st) + 1 To Int(nd) - 1
If WorksheetFunction.Weekday(T, 2) < 5 Then
totdiff = totdiff + TimeValue("8:00:00")
ElseIf WorksheetFunction.Weekday(T, 2) = 5 Then
totdiff = totdiff + TimeValue("4:45:00")
End If
Next T
End If

TimeDifference = totdiff

End Function
How to Use UDF code:
In the developer tab click--> Visual Basic
VB window opens
Insert--> Module
Paste the code.
Close the VB window.
Now UDF is available in Function List
This function can be used like other functions in excel.
Save file as .xlsm
 
Upvote 0
View attachment 116983
Formula in H3 and copied down
Excel Formula:
=TimeDifference($E3,$F3,$K$2,$K$3,$K$4,$L$2,$L$3,$M$2,$M$3)
VBA code for UDF(User Defined Function) is used.
Code:
Option Explicit


VBA Code:
Function TimeDifference(StartDt As Range, EndDt As Range, ShiftStart As Range, ShiftEnd1 As Range, ShiftEnd2 As Range, BrkStart1 As Range, BrkEnd1 As Range, _
                        BrkStart2 As Range, BrkEnd2 As Range)
Dim st#, nd#, stb1#, stb2#, ndb1#, ndb2#, Shst#, Shnd1#, shnd2#, stmd#, ndmd#, Shstmd#, shnd#
Dim totdiff#, diff1#, Days&, T&
Dim wf As WorksheetFunction
st = StartDt: nd = EndDt: stb1 = BrkStart1: stb2 = BrkStart2: ndb1 = BrkEnd1: ndb2 = BrkEnd2
Shst = ShiftStart: Shnd1 = ShiftEnd1: shnd2 = ShiftEnd1
stmd = st - Int(st): ndmd = nd - Int(nd)
Shstmd = Shst - Int(Shst)
totdiff = 0
Days = Int(nd) - Int(st) + 1

If Days = 1 Then
If WorksheetFunction.Weekday(Int(st), 2) < 5 Then shnd = Shnd1 Else shnd = shnd2
diff1 = WorksheetFunction.Median(shnd, Shst, ndmd) - WorksheetFunction.Median(shnd, Shst, stmd)
If stmd <= stb1 And ndmd >= ndb1 Then
totdiff = diff1 - TimeValue("00:30:00")
Else
totdiff = diff1
End If
If stmd <= stb2 And ndmd >= ndb2 And WorksheetFunction.Weekday(Int(st), 2) < 5 Then
totdiff = totdiff - TimeValue("00:30:00")
End If

End If

diff1 = 0

If Days >= 2 Then
If WorksheetFunction.Weekday(Int(st), 2) < 5 Then shnd = Shnd1 Else shnd = shnd2
diff1 = shnd - WorksheetFunction.Median(shnd, Shst, stmd)
If stmd <= stb1 Then
totdiff = diff1 - TimeValue("00:30:00")
Else
totdiff = diff1
End If
'K = WorksheetFunction.Weekday(45547, 2)
If stmd <= stb2 And WorksheetFunction.Weekday(Int(st), 2) < 5 Then
totdiff = totdiff - TimeValue("00:30:00")
End If
diff1 = 0

If WorksheetFunction.Weekday(Int(nd), 2) < 5 Then shnd = Shnd1 Else shnd = shnd2
diff1 = WorksheetFunction.Median(shnd, Shst, ndmd) - Shst
If ndmd >= stb1 Then
totdiff = totdiff + diff1 - TimeValue("00:30:00")
Else
totdiff = totdiff + diff1
End If
If ndmd >= stb2 And WorksheetFunction.Weekday(Int(nd), 2) < 5 Then
totdiff = totdiff - TimeValue("00:30:00")
End If

End If

If Days > 2 Then
For T = Int(st) + 1 To Int(nd) - 1
If WorksheetFunction.Weekday(T, 2) < 5 Then
totdiff = totdiff + TimeValue("8:00:00")
ElseIf WorksheetFunction.Weekday(T, 2) = 5 Then
totdiff = totdiff + TimeValue("4:45:00")
End If
Next T
End If

TimeDifference = totdiff

End Function
How to Use UDF code:
In the developer tab click--> Visual Basic
VB window opens
Insert--> Module
Paste the code.
Close the VB window.
Now UDF is available in Function List
This function can be used like other functions in excel.
Save file as .xlsm
Thank you, unfortunately I cannot use xlsm (online Sharepoint). I am thinking that my requirement is perhaps too complex for standard excel formulas.
 
Upvote 0
Would this work?:

I also consideres if time difference in negative. If that is never going to happen and if you need the time difference to be a number and not a text, then that is an easy fix.

U2:U3 is formatted as a table named T_Holidays where you can add all your holidays.
result in column G, H, I is formatted as "[h]:mm".

Book1.xlsx
ABCDEFGHIJKLMNOPQRSTU
1Online DateTarget Offline DateActual OfflineOnline To Target [h:mm]Online to Actual [h:mm]Difference [h:mm]
211/09/2024 07:3113/09/2024 14:5916/09/2024 08:3420:2922:332:04DaysShift StartShift endBreak Start 1Break end 1Break Start 2Break end 2Holidays
311/09/2024 09:1716/09/2024 10:0016/09/2024 10:4521:5822:430:45Mon-Thu06:3015:3009:3009:4512:3013:0017/09/2024
411/09/2024 11:1816/09/2024 11:4616/09/2024 11:4621:5821:580:00Fri06:3011:4509:3009:45
516/09/2024 15:2518/09/2024 06:3516/09/2024 15:300:100:05- 0:05
616/09/2024 15:2518/09/2024 06:3518/09/2024 06:550:100:300:20
716/09/2024 06:0016/09/2024 16:008:15
816/09/2024 06:0017/09/2024 16:008:15
916/09/2024 06:0018/09/2024 16:0016:30
1016/09/2024 06:0016/09/2024 16:00 8:15
1116/09/2024 06:0017/09/2024 16:00 8:15
1216/09/2024 06:0018/09/2024 16:00 16:30
1316/09/2024 09:2516/09/2024 09:5016/09/2024 09:510:100:110:01
1416/09/2024 12:2516/09/2024 13:0516/09/2024 13:060:100:110:01
Sheet1
Cell Formulas
RangeFormula
G2:I14G2=LET( timeBetween, LAMBDA(t_1,t_2, LET(n, ROUND((t_2-t_1)*24*60, 0), s, SEQUENCE(n,,t_1, TIME(0,1,0)), d, INT(s), m, VALUE(TEXT(s, "hh:mm")), isWorkingDay, WORKDAY.INTL(d-1, 1, 1, T_Holidays)=d, isWorkingHour, ((WEEKDAY(d, 2)<5)*(m>= $M$3)*(m<$N$3)*NOT((m>=$O$3)*(m<$P$3)+(m>=$Q$3)*(m<$R$3))+(WEEKDAY(d, 2)=5)*(m>= $M$4)*(m<$N$4)*NOT((m>=$O$4)*(m<$P$4)))>0, SUM(isWorkingDay*isWorkingHour)/24/60 )), onlineToTargetOffline, timeBetween(D2, E2), onlineToActualOffline, timeBetween(D2, F2), dif, onlineToActualOffline-onlineToTargetOffline, HSTACK(IFERROR(onlineToTargetOffline, ""), IFERROR(onlineToActualOffline, ""), IFERROR(IF(dif<0, "- ", "") & TEXT(ABS(dif), "[h]:mm"), "")) )
Dynamic array formulas.


Let me know if this works for you.
Thank you, unfortunately I cannot use xlsm (online Sharepoint). I am thinking that my requirement is perhaps too complex for standard excel formulas.
 
Upvote 0
Thank you, unfortunately I cannot use xlsm (online Sharepoint). I am thinking that my requirement is perhaps too complex for standard excel formulas.
Felix's answer doesn't use VBA, it is a formula-only approach, using the newer "LET" function.

Which version of Excel are you using here, 365 or 2019?
The "LET" function he uses is not available for Excel 2019.
 
Upvote 0
Thank you, unfortunately I cannot use xlsm (online Sharepoint). I am thinking that my requirement is perhaps too complex for standard excel formulas.
As Joe4 explained, it is just a formula which should work if you are using the web version of excel.
 
Upvote 0
Felix's answer doesn't use VBA, it is a formula-only approach, using the newer "LET" function.

Which version of Excel are you using here, 365 or 2019?
The "LET" function he uses is not available for Excel 2019.
Ah okay thanks - I am using 365 - I will try it!
 
Upvote 0

Forum statistics

Threads
1,223,794
Messages
6,174,637
Members
452,575
Latest member
Fstick546

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