[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# Calculate Working Days between two dates

Posted on 2008-06-11
Medium Priority
2,021 Views
Hi all.

I'm working on an Access 2003 query that will display how many working days it took to complete an order.

There are 2 dates: PromiseDate (or BegDate) and CompletionDate (or EndDate)

I also have a table called Holidays, which holds holiday dates (i.e. 12/25/2008, 01/01/2008) so they do not get included in the calculation and therefore not count against the length of time it took to complete an order...so it simply calculates the working days. I've been using the following function:
***************************************************************************************************************************
Function Work_Days(BegDate As Variant, EndDate As Variant) As Integer

Dim WholeWeeks As Variant
Dim DateCnt As Variant
Dim EndDays As Integer

BegDate = DateValue(BegDate)
EndDate = DateValue(EndDate)
DateCnt = BegDate
EndDays = 0
Do While DateCnt < EndDate

If Not IsNull(DLookup("HoliDate", "Holidays", "[HoliDate]=#" & DateCnt & "#")) Then
EndDays = EndDays - 1
End If

If Format(DateCnt, "ddd") <> "Sun" And _
Format(DateCnt, "ddd") <> "Sat" Then
EndDays = EndDays + 1
End If

Loop

Work_Days = EndDays

End Function
*******************************************************************************************************************************
But as you can see, the loop condition is while the PromiseDate is before the CompletionDate, there may be times when we complete an order before the promise date.

In other words, let's say we promised to complete an order on 10/15/2008, but we actually complete the order on 10/14/2008. Then the calculated work days it took to complete it was -1.

Currently the query will display 0.

There are two parts where I'm having difficulty:

1. The loop condition (what if the promise date is larger than the completion date, as in the example above)
2. DateCnt = DateAdd("d", 1, DateCnt) --> Using the above example, it should subtract a day instead of add one, how do I do that?

0
Question by:printmedia
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2
• +1

LVL 5

Accepted Solution

scgstuff earned 2000 total points
ID: 21764474
Try this:

Shawn

``````Function Work_Days(BegDate As Variant, EndDate As Variant) As Integer

Dim WholeWeeks As Variant
Dim DateCnt As Variant
Dim EndDays As Integer

BegDate = DateValue(BegDate)
EndDate = DateValue(EndDate)
DateCnt = BegDate
EndDays = 0
If DateCnt < EndDate Then
Do While DateCnt < EndDate

If Not IsNull(DLookup("HoliDate", "Holidays", "[HoliDate]=#" & DateCnt & "#")) Then
EndDays = EndDays - 1
End If

If Format(DateCnt, "ddd") <> "Sun" And _
Format(DateCnt, "ddd") <> "Sat" Then
EndDays = EndDays + 1
End If

Loop

Else

Do While DateCnt > EndDate

If Not IsNull(DLookup("HoliDate", "Holidays", "[HoliDate]=#" & DateCnt & "#")) Then
EndDays = EndDays - 1
End If

If Format(DateCnt, "ddd") <> "Sun" And _
Format(DateCnt, "ddd") <> "Sat" Then
EndDays = EndDays + 1
End If

Loop

End If

Work_Days = EndDays

End Function
``````
0

LVL 5

Expert Comment

ID: 21764484
This will allow thie function to subtract if it is a larger promise date.  The reason it was returning 0 is you set it to 0 and then only allowed the loop if the promise date was smaller, so it never actually executed that part of the loop.

Shawn
0

LVL 44

Expert Comment

ID: 21765486
There isn't a StartDate?
0

LVL 44

Expert Comment

ID: 21765503
With a StartDate, EndDate, and CompletionDate, do you also want to know the status of those projects which are underway, or only the status of those projects which are completed - in which case the EndDate is irrelevant?  What I call adding smoke;-)
0

LVL 52

Expert Comment

ID: 21766992
Here's the function we use. It has the advantage that it accepts reversed input dates and it works in an international environment. Also, should you need it, it is much faster for large periods.

To exclude the holidays, simply look up these for the period in question, count those that fall on workdays and deduct that count. This is much faster than looking up each and every day:

lngWorkdaysOfHolidays = DCount("*", "[Holidays]", "[Holidate] Between #" & BegDate & "# And #" & EndDate & "# And Weekday([Holidate], 2) < 6")

To be absolutely precise you may need to exclude those situations where both BegDate and EndDate fall and the same holiday as this would return a negative total of workdays, or you could perform the counting only for a positive count of workdays:

lngWorkdays = ISO_WorkdayDiff(BegDate, EndDate)
If lngWorkdays > 0 Then
lngWorkdays = lngWorkdays - lngWorkdaysOfHolidays
End If

/gustav
``````Public Function ISO_WorkdayDiff( _
ByVal datDateFrom As Date, _
ByVal datDateTo As Date) _
As Long

' Purpose: Calculate number of working days between dates datDateFrom and datDateTo.
' Assumes: 5 or 6 working days per week. Weekend is (Saturday and) Sunday.
' Limitation: Does not count for public holidays.
' May be freely used and distributed.
' 1999-04-23. Gustav Brock, Cactus Data ApS, Copenhagen
'             Option for 5 or 6 working days per week added.

Const cbytWorkdaysOfWeek  As Byte = 5

Dim bytSunday             As Byte
Dim intWeekdayDateFrom    As Integer
Dim intWeekdayDateTo      As Integer
Dim lngDays               As Long
Dim datDateTemp           As Date

' Reverse dates if these have been input reversed.
If datDateFrom > datDateTo Then
datDateTemp = datDateFrom
datDateFrom = datDateTo
datDateTo = datDateTemp
End If

' Find ISO weekday for Sunday.
bytSunday = WeekDay(vbSunday, vbMonday)

' Find weekdays for the dates.
intWeekdayDateFrom = WeekDay(datDateFrom, vbMonday)
intWeekdayDateTo = WeekDay(datDateTo, vbMonday)

' Compensate weekdays' value for non-working days (weekends).
intWeekdayDateFrom = intWeekdayDateFrom + (intWeekdayDateFrom = bytSunday)
intWeekdayDateTo = intWeekdayDateTo + (intWeekdayDateTo = bytSunday)

' Calculate number of working days between the two weekdays, ignoring number of weeks.
lngDays = intWeekdayDateTo - intWeekdayDateFrom - (cbytWorkdaysOfWeek * (intWeekdayDateTo < intWeekdayDateFrom))
' Add number of working days between the weeks of the two dates.
lngDays = lngDays + (cbytWorkdaysOfWeek * DateDiff("w", datDateFrom, datDateTo, vbMonday, vbFirstFourDays))

ISO_WorkdayDiff = lngDays

End Function
``````
0

LVL 52

Expert Comment

ID: 21767207
It could be done this way.

/gustav
``````Public Function ISO_WorkdayDiff( _
ByVal datDateFrom As Date, _
ByVal datDateTo As Date, _
Optional ByVal booExcludeHolidays As Boolean) _
As Long

' Purpose: Calculate number of working days between dates datDateFrom and datDateTo.
' Assumes: 5 or 6 working days per week. Weekend is (Saturday and) Sunday.
' May be freely used and distributed.

' 1999-04-23. Gustav Brock, Cactus Data ApS, Copenhagen
'             Option for 5 or 6 working days per week added.
' 2008-06-12. Option to exclude holidays from the count of workdays.

Const cbytWorkdaysOfWeek  As Byte = 5
' Name of table with holidays.
Const cstrTableHoliday    As String = "tblHoliday"
' Name of date field in holiday table.
Const cstrFieldHoliday    As String = "HolidayDate"

Dim bytSunday             As Byte
Dim intWeekdayDateFrom    As Integer
Dim intWeekdayDateTo      As Integer
Dim lngDays               As Long
Dim datDateTemp           As Date
Dim strDateFrom           As String
Dim strDateTo             As String
Dim lngHolidays           As Long
Dim strFilter             As String

' Reverse dates if these have been input reversed.
If datDateFrom > datDateTo Then
datDateTemp = datDateFrom
datDateFrom = datDateTo
datDateTo = datDateTemp
End If

' Find ISO weekday for Sunday.
bytSunday = WeekDay(vbSunday, vbMonday)

' Find weekdays for the dates.
intWeekdayDateFrom = WeekDay(datDateFrom, vbMonday)
intWeekdayDateTo = WeekDay(datDateTo, vbMonday)

' Compensate weekdays' value for non-working days (weekends).
intWeekdayDateFrom = intWeekdayDateFrom + (intWeekdayDateFrom = bytSunday)
intWeekdayDateTo = intWeekdayDateTo + (intWeekdayDateTo = bytSunday)

' Calculate number of working days between the two weekdays, ignoring number of weeks.
lngDays = intWeekdayDateTo - intWeekdayDateFrom - (cbytWorkdaysOfWeek * (intWeekdayDateTo < intWeekdayDateFrom))
' Add number of working days between the weeks of the two dates.
lngDays = lngDays + (cbytWorkdaysOfWeek * DateDiff("w", datDateFrom, datDateTo, vbMonday, vbFirstFourDays))

If booExcludeHolidays And lngDays > 0 Then
strDateFrom = Format(datDateFrom, "yyyy\/mm\/dd")
strDateTo = Format(datDateTo, "yyyy\/mm\/dd")
strFilter = cstrFieldHoliday & " Between #" & strDateFrom & "# And #" & strDateTo & "# And Weekday(" & cstrFieldHoliday & ", 2) <= " & cbytWorkdaysOfWeek & ""
lngHolidays = DCount("*", cstrTableHoliday, strFilter)
End If

ISO_WorkdayDiff = lngDays - lngHolidays

End Function
``````
0

Author Comment

ID: 21771715
Thanks for all your help, but I went with scgstuff's answer since it's the same function we've used until now.

scgstuff, I changed the function a bit:

Do While DateCnt > EndDate

If Not IsNull(DLookup("HoliDate", "Holidays", "[HoliDate]=#" & DateCnt & "#")) Then
EndDays = EndDays + 1
End If

If Format(DateCnt, "ddd") <> "Sun" And _
Format(DateCnt, "ddd") <> "Sat" Then
EndDays = EndDays - 1
End If

Loop

******************************************************************************************************************************
So that way if the calculated days is < 0, when know it was completed before the scheduled promise date.

Thanks again!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
###### Suggested Courses
Course of the Month13 days, 20 hours left to enroll