Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #105
Online Version at http://www.codeoftheweek.com/membersonly/bi/0105.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 future dates using business date rules

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

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

Requirements

In this Issue

This issue is an extension of issue 104. It shows how to calculate the date which is some number of workdays away from a particular date. 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 HolidayExists(dDate As Date) As Boolean

Returns True if the date passed in as dDate is a valid holiday based on the list created by HolidayAdd.

Public Function BusinessDayCount(ByVal dDate As Date, ByVal iDays As Integer) As Date

Returns the date that is iDays business days from dDate. The default business day rules are that today counts as the first business day and Saturday and Sunday are not business days. You can modify this behavior using the IncludeSaturdays, IncludeSundays, and IncludeFirstDate properties.

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

Setting this property to False will omit the first 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 business date calculation routines 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 calculates the date which is 5 days after 12/31/98. It shows some of the options you can use and provides the sample output after the sample source code.

Private Sub DateTest()
   Dim BusDay As New cBusinessDates
   Dim iDayCount As Integer
   Dim dStartDate As Date

   BusDay.HolidayAdd "1/1/99"
   iDayCount = 5
   dStartDate = "12/31/98"
   BusDay.IncludeFirstDate = False
   Debug.Print iDayCount & " business days from " & dStartDate & " is " & _
          BusDay.BusinessDayCount(dStartDate, iDayCount) & " not including " & dStartDate
   BusDay.IncludeFirstDate = True
   Debug.Print iDayCount & " business days from " & dStartDate & " is " & _
          BusDay.BusinessDayCount(dStartDate, iDayCount) & " including " & dStartDate
End Sub
Sample output from the above routine.
5 business days from 12/31/98 is 1/8/99 not including 12/31/98
5 business days from 12/31/98 is 1/7/99 including 12/31/98

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/0105.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