Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #104
Online Version at http://www.codeoftheweek.com/membersonly/bi/0104.html (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Issue topic: Calculating workdays between two dates

Get paid to surf the web!

If you would like to get paid for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html

Six months of VB Training for only $49.99

Want to get up to speed on the latest Visual Basic programming? Includes Visual Basic 6 and Visual InterDev 6. Check out our training programs at http://www.codeoftheweek.com/vbtraining.html

Requirements

In this Issue

This issue shows how to calculate the number of workdays between two dates. It has several options for controlling how Saturday and Sunday are included. It also allows you to add a list of holidays to include in the calculations.

If you have any questions about using this module, let us know at questions@codeoftheweek.com

Functions

Public Function BusinessDateDiff(ByVal dDate1 As Date, ByVal dDate2 As Date) As Long

Returns the number of business days (or workdays) between dDate1 and dDate2. It has various properties which can be set to control how the calculation is performed. These properties are detailed below.

Properties

Public IncludeSaturdays As Boolean

When True the calculation will include any days that are Saturdays. When False it will omit any Saturdays from the calculation.

Public IncludeSundays As Boolean

When True the calculation will include any days that are Sundays. When False it will omit any Sundays from the calculation.

Public Property Let IncludeFirstDate(bInc As Boolean)
Public Property Get IncludeFirstDate() As Boolean

By default the BusinessDateDiff routine includes the first date in the range in the calculation, but does not include the last day. Setting this property to False will omit the first date in the range from the calculation.

Public Property Let IncludeLastDate(bInc As Boolean)
Public Property Get IncludeLastDate() As Boolean

By default the BusinessDateDiff routine does not include the last date in the range in the calculation. Setting this property to True will include the last date in the range from the calculation.

Methods

Public Sub HolidayAdd(dHoliday As Date)

Add a holiday to the list of dates to omit from the calculation. By default there are no holidays added so the BusinessDateDiff routine will count all dates.

Public Sub HolidayRemove(dHoliday As Date)

Removes a holiday from the list of dates to omit from the calculation.

Public Sub HolidayClear()

Clears the complete list of holidays added with HolidayAdd.

Sample Usage

This example accepts two dates and performs several calculations with the various options turned on and off. The results that should be returned for the dates 1/1/99 and 1/31/99 are shown below:

 29
 30
 31
 30
 25
 20
   Function WorkdayTest(dDate1 as Date, dDate2 as Date)
      Dim BusDay As New cBusinessDates

      BusDay.IncludeSaturdays = True
      BusDay.IncludeSundays = True
      BusDay.HolidayAdd "1/1/99"
      BusDay.HolidayAdd "7/4/99"
      BusDay.HolidayAdd "12/25/99"
      BusDay.HolidayAdd "12/31/99"
      Debug.Print BusDay.BusinessDateDiff(dDate1, dDate2)
      BusDay.HolidayRemove "1/1/99"
      Debug.Print BusDay.BusinessDateDiff(dDate1, dDate2)
      BusDay.IncludeLastDate = True
      Debug.Print BusDay.BusinessDateDiff(dDate1, dDate2)
      BusDay.IncludeFirstDate = False
      Debug.Print BusDay.BusinessDateDiff(dDate1, dDate2)
      BusDay.IncludeSaturdays = False
      Debug.Print BusDay.BusinessDateDiff(dDate1, dDate2)
      BusDay.IncludeSundays = False
      Debug.Print BusDay.BusinessDateDiff(dDate1, dDate2)
   Exit Function

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0104.html


This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Get paid to surf the web!

If you would like to get paid for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945
Email: info@codeoftheweek.com
Web: http://www.codeoftheweek.com