Option Explicit
Function DDouglasDateDiff(ByVal dDate1 As Date, ByVal dDate2 As Date) As Double
Dim dbTemp As Double
Dim lDay As Long
Dim lCounter As Long
'Move dDate1 forward to 8 AM on the same day,
'if its time portion is before 8
If DatePart("h", dDate1) < 8 Then
dDate1 = CDate(Left(CStr(dDate1), InStr(1, CStr(dDate1), " ") - 1) & " 08:00:00")
End If
'Move dDate1 forward to 8 AM on the next day,
'if its time portion is after 5
If DatePart("h", dDate1) >= 17 Then
dDate1 = CDate(Left(CStr(dDate1 + 1), InStr(1, CStr(dDate1 + 1), " ") - 1) & " 08:00:00")
End If
'Move dDate1 forward if it is a weekend
Do While Weekday(dDate1, vbMonday) > 5
dDate1 = CDate(Left(CStr(dDate1 + 1), InStr(1, CStr(dDate1 + 1), " ") - 1) & " 08:00:00")
Loop
'Move dDate2 back to 5 PM on the same day,
'if its time portion is after 5
If DatePart("h", dDate2) >= 17 Then
dDate1 = CDate(Left(CStr(dDate2), InStr(1, CStr(dDate2), " ") - 1) & " 17:00:00")
End If
'Move dDate2 back to 5 PM on the previous day,
'if its time portion is before 8
If DatePart("h", dDate2) < 8 Then
dDate1 = CDate(Left(CStr(dDate2 - 1), InStr(1, CStr(dDate2 - 1), " ") - 1) & " 17:00:00")
End If
'Move dDate2 backward if it is a weekend
Do While Weekday(dDate2, vbMonday) > 5
dDate1 = CDate(Left(CStr(dDate2 - 1), InStr(1, CStr(dDate2 - 1), " ") - 1) & " 08:00:00")
Loop
'Get the number of seconds between the dates
dbTemp = DateDiff("s", dDate1, dDate2)
'Get the number of days between the dates
lDay = DateDiff("d", dDate1, dDate2)
If lDay <> 0 Then
For lCounter = 1 To lDay
If Weekday(dDate1 + lCounter, vbMonday) > 5 Then
'For weekends remove 24 hours
dbTemp = dbTemp - 86400 '(24 hrs * 60 mins * 60 secs)
Else
'For all other days, remove 15 hours (5PM - 8AM)
dbTemp = dbTemp - 54000 '(15 hrs * 60 mins * 60 secs)
End If
Next lCounter
End If
'Convert to minutes
DDouglasDateDiff = dbTemp / 60
End Function