Code:
Const cBeginTime As Date = #8:00:00 AM#
Const cEndTime As Date = #8:00:00 PM#
Dim intCountDays As Integer
Dim intHours As Integer
Dim intMinutes As Integer
Dim intSeconds As Integer
Dim lngTotalSecs As Long
Dim dtStart As Date
Dim dtEnd As Date
Dim dtTemp As Date
Dim tmStart As Date
Dim tmEnd As Date
intMinutes = 0
intHours = 0
intSeconds = 0
lngTotalSecs = 0
intCountDays = 0
'check end date > start date
If EndDate < StartDate Then
dtTemp = StartDate
StartDate = EndDate
EndDate = dtTemp
End If
'get just the date portion
dtStart = Int(StartDate)
dtEnd = Int(EndDate)
'get just the time portion
tmStart = StartDate - dtStart
tmEnd = EndDate - dtEnd
'check start and end times are valid
'If Not (tmStart >= cBeginingTime And tmStart <= cEndingTime) Then
'MsgBox "Invalid start time. Start time before " & cBeginingTime
' Exit Function
'ElseIf Not (tmEnd >= cBeginingTime And tmEnd <= cEndingTime) Then
'MsgBox "Invalid end time. End time after " & cEndingTime
' Exit Function
'End If
If dtStart = dtEnd And (tmStart > cEndTime Or tmEnd < cBeginTime) Then
WorkingHrsMinsSecs = "0:0:0"
Else
'adjust times if before or after 8am and 8pm
If tmStart < cBeginTime Then tmStart = DateAdd("s", DateDiff("s", tmStart, cBeginTime), tmStart)
If tmEnd > cEndTime Then tmEnd = DateAdd("s", -1 * DateDiff("s", cEndTime, tmEnd), tmEnd)
If dtStart = dtEnd Then
lngTotalSecs = DateDiff("s", tmStart, tmEnd)
Else
'skip the first day
dtStart = dtStart + 1
Do While dtStart < dtEnd
'Make the above < and not <= to not count the EndDate
Select Case Weekday(dtStart)
Case Is = 1, 7
'do nothing
Case Is = 2, 3, 4, 5, 6
intCountDays = intCountDays + 1
End Select
dtStart = dtStart + 1
Loop
intHours = intCountDays * 720
'first day minutes
lngTotalSecs = lngTotalSecs + DateDiff("s", tmStart, cEndTime)
'Last day minutes
lngTotalSecs = lngTotalSecs + DateDiff("s", cBeginTime, tmEnd)
End If
intHours = intHours + lngTotalSecs \ 3600 'hours
intMinutes = Format(Int((lngTotalSecs Mod 3600) / 60), "00") 'minutes
intSeconds = Format(Int((lngTotalSecs Mod 3600) Mod 60), "00") 'seconds
'return value
WorkingHrsMinsSecs = intHours & ":" & intMinutes & ":" & intSeconds
End If