I found the following function on the net to calculate hours between 2 dates (EXCLUDING WEEKENDS).
The problem I am having is that it is returning 44 hours between the following dates where it should be 20-21 hours.
StartDate: 29/04/2016 2:54:53 PM
EndDate: 2/05/2016 11:18:21 AM
Looking at the calendar there would be around 9-10 hours on 29th from 2pm-12pm, no hours on Sat 30th APRIL or Sun 1st MAY, then 11 hours on 2nd May.
It looks like it is including 1 of the weekend days which would be 20+ 24 =44
The problem I am having is that it is returning 44 hours between the following dates where it should be 20-21 hours.
StartDate: 29/04/2016 2:54:53 PM
EndDate: 2/05/2016 11:18:21 AM
Looking at the calendar there would be around 9-10 hours on 29th from 2pm-12pm, no hours on Sat 30th APRIL or Sun 1st MAY, then 11 hours on 2nd May.
It looks like it is including 1 of the weekend days which would be 20+ 24 =44
Code:
Public Function WorkingHrsMins(StartDate As Date, EndDate As Date) As String
'....................................................................
' Name: WorkingHrs
' Inputs: StartDate As Date & time
' EndDate As Date & time
' Returns: Integer - number of hours not inclusive of weekends
' Author: Arvin Meyer
' Date: February 19, 1997
' Modified by Steve S (ssanfu)
' Date: April 11, 2012
' Comment: Accepts two dates and returns the number
' of work hours and minutes between 8am and 8pm
' Note that this function does not account for holidays.
'....................................................................
On Error GoTo Err_WorkingHrs
Const cBeginingTime As Date = #8:00:00 AM#
Const cEndingTime As Date = #8:00:00 PM#
Dim intCountDays As Integer
Dim intHours As Integer
Dim intMinutes As Integer
Dim intTotalMin As Integer
Dim dtStart As Date
Dim dtEnd As Date
Dim dtTemp As Date
Dim tmStart As Date
Dim tmEnd As Date
intMinutes = 0
intHours = 0
intCountDays = 0
intTotalMin = 0
WorkingHrsMins = 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 Then
intTotalMin = DateDiff("n", 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
intTotalMin = intCountDays * 720
'first day minutes
intTotalMin = intTotalMin + DateDiff("n", tmStart, cEndingTime)
'Last day minutes
intTotalMin = intTotalMin + DateDiff("n", cBeginingTime, tmEnd)
End If
intHours = intTotalMin \ 60 'hours
intMinutes = intTotalMin Mod 60 'minutes
'return value
WorkingHrsMins = intHours & " hrs " & intMinutes & " min"
Exit_WorkingHrs:
Exit Function
Err_WorkingHrs:
Select Case Err
Case Else
MsgBox Err.Description
Resume Exit_WorkingHrs
End Select
End Function
Last edited: