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
If you would like to get paid for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html
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
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.
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.
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.
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 SubSample 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
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
If you would like to get paid for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html