Solved

Calculate Working Days between two dates

Posted on 2008-06-11
7
2,006 Views
Last Modified: 2009-05-11
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
     
   
      DateCnt = DateAdd("d", 1, DateCnt)
   
      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?

Thank you in advance!


0
Comment
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
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 5

Accepted Solution

by:
scgstuff earned 500 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
      
   
      DateCnt = DateAdd("d", 1, DateCnt)
   
      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
      
   
      DateCnt = DateAdd("d", -1, DateCnt)
   
      Loop
 
      End If
      
      Work_Days = EndDays
 
End Function

Open in new window

0
 
LVL 5

Expert Comment

by:scgstuff
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

by:GRayL
ID: 21765486
There isn't a StartDate?
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 44

Expert Comment

by:GRayL
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 50

Expert Comment

by:Gustav Brock
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
' 2000-10-03. Constants added.
'             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

Open in new window

0
 
LVL 50

Expert Comment

by:Gustav Brock
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
' 2000-10-03. Constants added.
'             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

Open in new window

0
 

Author Comment

by:printmedia
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
     
   
      DateCnt = DateAdd("d", -1, DateCnt)
   
      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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question