Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Will Rickards http://willrickards.net/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

work days between two dates without cycling through dates

I was thinking about how to optimize figuring out the work days between two dates and came up with this function. It doesn't take into account holidays. You would have to take out the workdays for holidays from the number if you want to take into account holidays, but that should be easy enough.

4GL Version
   1   
   2  # workdays
   3  # returns the number of working days between two dates
   4  FUNCTION workdays( dt_begin, dt_end )
   5  
   6  DEFINE
   7     dt_begin             DATE,
   8     dt_end               DATE,
   9     dt_first_sunday      DATE,
  10     dt_last_saturday     DATE,
  11     int_workdays         INTEGER
  12  
  13     # get first sunday
  14     LET dt_first_sunday = dt_begin + ((7 - WEEKDAY(dt_begin)) MOD 7)
  15  
  16     # get last saturday
  17     LET dt_last_saturday = dt_end + ((-1 * (WEEKDAY(dt_end) + 1)) MOD 7)
  18  
  19     # get work weeks between first sunday and last saturday
  20     LET int_workdays = (((dt_last_saturday - dt_first_sunday) + 1) / 7) * 5
  21     
  22     # if first sunday is not begin date
  23     IF dt_first_sunday <> dt_begin THEN
  24  
  25        # assume first sunday is after begin date
  26        # add workdays from begin date to first sunday
  27        LET int_workdays = int_workdays + (6 - WEEKDAY(dt_begin))
  28  
  29     END IF
  30  
  31     # if last saturday is not end date
  32     IF dt_last_saturday <> dt_end THEN
  33  
  34        # assume last saturday is before end date
  35        # add workdays from last saturday to end date
  36        LET int_workdays = int_workdays + WEEKDAY(dt_end)  
  37    
  38     END IF
  39  
  40     # return working days
  41     RETURN int_workdays
  42  
  43  END FUNCTION


VBA Version
   1  
   2  ' WorkDays
   3  ' returns the number of working days between two dates
   4  Public Function WorkDays(ByVal dtBegin As Date, ByVal dtEnd As Date) As Long
   5  
   6     Dim dtFirstSunday As Date
   7     Dim dtLastSaturday As Date
   8     Dim lngWorkDays As Long
   9  
  10     ' get first sunday in range
  11     dtFirstSunday = dtBegin + ((8 - Weekday(dtBegin)) Mod 7)
  12  
  13     ' get last saturday in range
  14     dtLastSaturday = dtEnd - (Weekday(dtEnd) Mod 7)
  15  
  16     ' get work days between first sunday and last saturday
  17     lngWorkDays = (((dtLastSaturday - dtFirstSunday) + 1) / 7) * 5
  18  
  19     ' if first sunday is not begin date
  20     If dtFirstSunday <> dtBegin Then
  21  
  22        ' assume first sunday is after begin date
  23        ' add workdays from begin date to first sunday
  24        lngWorkDays = lngWorkDays + (7 - Weekday(dtBegin))
  25  
  26     End If
  27  
  28     ' if last saturday is not end date
  29     If dtLastSaturday <> dtEnd Then
  30  
  31        ' assume last saturday is before end date
  32        ' add workdays from last saturday to end date
  33        lngWorkDays = lngWorkDays + (Weekday(dtEnd) - 1)
  34  
  35     End If
  36  
  37     ' return working days
  38     WorkDays = lngWorkDays
  39  
  40  End Function
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS