Not Weekend - Calc Using 5 Day Week - Validate Date - December 2008

This months tip shows how to test a date for a weekend date, how to calculate days excluding weekends and how to validate a date. The VB.Net code is contained in class modules:

cNotWeekend.vb
cCalcUsing5DayWeek.vb
cDates.vb

2009/02/06 - I created a web project to demonstrate the use of the functions:

.cCalcUsing5DayWeek - Enter valid dates or a duration, select which is to be calculated then click the 'Calc' button. Two of the 3 items must be entered. If missing or invalid the item is shown in red text.

.NotWeekend - Click the calendar on a date. If it is a Saturday or Sunday then the following Monday is selected. Check 'Set to Friday' to cause the prior Friday to be selected. Clicking on a week day does nothing.

Using Not Weekend

NotWeekend(dDate [,bPrior])

Pass a date to this function. If the date is a Saturday or Sunday then the next Monday is returned unless bPrior is True in which case the preceding Friday is returned. Examples:

Dim o As New cNotWeekend
Dim dNotWeekend As Date = o.NotWeekend(Today)
If Today is a Saturday or Sunday then the following Monday's date is returned,
otherwise Today is returned.
Dim o As New cNotWeekend
Dim dNotWeekend As Date = o.NotWeekend(Today, True)
If Today is a Saturday or Sunday then the prior Friday's date is returned,
otherwise Today is returned.

Using CalcUsing5DayWeek

Use these routines to pass an Enum and 2 variables to the routine and have the third calculated. For example: pass eDuration and a start and end date and the routine will calculate the number of non-weekend days between the two dates. Pass eFinish and a start date and duration and the finish date is calculated. Pass eStart and a duration and finish date and the start date is calculated. All calculations exclude weekend days.

Dim oCalc As New cCalcUsing5DayWeek
Dim dFinish As Date
If oCalc.CalcUsing5DayWeek(eFinish, 5, Today, dFinish) Then
   ...
End If
Returns the finish date, 5 non-weekend days from Today. If provided, the start date must be earlier than the finish date and contain valid dates. If invalid then False is returned, otherwise True is returned.

Enum Values:

Public Enum enumCalc
   eFinish
   eStart
   eDuration
End Enum

Using Date Validation

Date validation is described in the September 2008 tip-of-the-month. Click here to review the tip. The code is included in this month's tip because cCalcUsing5DayWeek uses the date validation routines.

Download VB.Net Code

Download - VS2005 VB.Net Zipped File  Size 
Project with a Solution File 5DayWeekSLN.zip 34k

Previous Tip   Tip Index   Next Tip

Home

Updated 2010/12/28