Introduction
Have you ever had to add days to a date and then make sure that the days are all business days?
Well, I have had to do this a number of times and when a junior developer asked me how I would do this, I thought I would share it with others. This will only work for a standard work week. By this, I mean Monday
to Friday
. Saturday
and Sunday
will be considered the weekend. The code is pretty easy to understand and if need be, should be simple to modify for your own particular use.
Using the Code
To simply add 2 business weeks to a start date, you call the function CalculateTenBusinessDaysFromInputDate
and pass it the StartDate
. This function will return you a date that has 10 business days from the StartDate
. If the Startdate
is a Saturday
, we simply add 16 days to the StartDate
passed in. If the StartDate
is on a Sunday
, then we add 15 days to the StartDate
. For any other day of the week, we just add 14 days to the StartDate
.
VB.NET
Public Function CalculateTenBusinessDaysFromInputDate(ByVal StartDate As Date) As Date
Select Case StartDate.DayOfWeek
Case DayOfWeek.Sunday
Return StartDate.AddDays(15)
Case DayOfWeek.Monday, DayOfWeek.Tuesday, _
DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday
Return StartDate.AddDays(14)
Case DayOfWeek.Saturday
Return StartDate.AddDays(16)
Case Else
Return StartDate
End Select
End Function
C#
public System.DateTime CalculateTenBusinessDaysFromInputDate(System.DateTime StartDate)
{
switch (StartDate.DayOfWeek)
{
case DayOfWeek.Sunday:
return StartDate.AddDays(15);
break;
case DayOfWeek.Monday:
case DayOfWeek.Tuesday:
case DayOfWeek.Wednesday:
case DayOfWeek.Thursday:
case DayOfWeek.Friday:
return StartDate.AddDays(14);
break;
case DayOfWeek.Saturday:
return StartDate.AddDays(16);
break;
default:
return StartDate;
}
}
This bit of code is a little bit more realistic. To use the CalculateBusinessDaysFromInputDate
function, you need to supply both a StartDate
and the number of business days that you want added to the StartDate
. In CalculateBusinessDaysFromInputDate
, we first check to see if the StartDate
is a Saturday
or a Sunday
. If so, then I knock the number of business days down by one. I do this because we don't want the resulting date to be 11 business days out. NOTE: I did not do this in the CalculateTenBusinessDaysFromInputDate
. For instance if the StartDate
is a Monday
and we added 12 business days to it, then the result should be on a Wednesday. Before we return the resulting date, we need to make sure that the date is not a weekend. If it is, then I move the date to the next Monday
. You can also move the date back to the preceeding Friday
by simply using the .AddDays() Date
function and pass in a negative number. i.e. .AddDays(-2)
.
VB.NET
Public Function CalculateBusinessDaysFromInputDate_
(ByVal StartDate As Date, ByVal NumberOfBusinessDays As Integer) As Date
If StartDate.DayOfWeek = DayOfWeek.Saturday Or StartDate.DayOfWeek = _
DayOfWeek.Sunday Then
NumberOfBusinessDays -= 1
End If
For index = 1 To NumberOfBusinessDays
Select Case StartDate.DayOfWeek
Case DayOfWeek.Sunday
StartDate = StartDate.AddDays(2)
Case DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, _
DayOfWeek.Thursday, DayOfWeek.Friday
StartDate = StartDate.AddDays(1)
Case DayOfWeek.Saturday
StartDate = StartDate.AddDays(3)
End Select
Next
If StartDate.DayOfWeek = DayOfWeek.Saturday Then
StartDate = StartDate.AddDays(2)
ElseIf StartDate.DayOfWeek = DayOfWeek.Sunday Then
StartDate = StartDate.AddDays(1)
End If
Return StartDate
End Function
C#
public System.DateTime CalculateBusinessDaysFromInputDate
(System.DateTime StartDate, int NumberOfBusinessDays)
{
if (StartDate.DayOfWeek == DayOfWeek.Saturday |
StartDate.DayOfWeek == DayOfWeek.Sunday)
{
NumberOfBusinessDays -= 1;
}
int index = 0;
for (index = 1; index <= NumberOfBusinessDays; index++)
{
switch (StartDate.DayOfWeek)
{
case DayOfWeek.Sunday:
StartDate = StartDate.AddDays(2);
break;
case DayOfWeek.Monday:
case DayOfWeek.Tuesday:
case DayOfWeek.Wednesday:
case DayOfWeek.Thursday:
case DayOfWeek.Friday:
StartDate = StartDate.AddDays(1);
break;
case DayOfWeek.Saturday:
StartDate = StartDate.AddDays(3);
break;
}
}
if (StartDate.DayOfWeek == DayOfWeek.Saturday)
{
StartDate = StartDate.AddDays(2);
}
else if (StartDate.DayOfWeek == DayOfWeek.Sunday)
{
StartDate = StartDate.AddDays(1);
}
return StartDate;
}
Points of Interest
This is a very simple way to calculate business days. There is no big algorithm associated with it.
If you need to figure out days in the past, this will work as well. I hope this helps in some small way.
History
- 28th October, 2009: Initial version