# Calculate Business days in ASP.NET C# | CodeAsp.Net

## Calculate Business days in ASP.NET C#

/5

Many times we want to find out the next business (working) date after “N” business days. In this blog I have described how to find out the next business date after “N” business days.
Here “N” is the number of business days.
Business days means remove all the Sunday and Saturday which falls between “N” business days from the current date(Or from specified date).
Below given method find out, that current day is Saturday or Sunday i.e find out the WeekEnd.

public static bool IsWeekEnd(DateTime dateTime)

{

bool isWeekEnd = false;

switch (dateTime.DayOfWeek)

{

case DayOfWeek.Sunday:

case DayOfWeek.Saturday:

isWeekEnd = true;

break;

}

return isWeekEnd;

}

This method returns true if the current date is Satuarday or Sunday otherwise return false.
Suppose we want to find out the date which comes after “N” business days from the give date.

{

//This is used to count the number of business days

for (int i = 1; i <= numberOfBusinessDays; i++)

{

//if current date is the WeekEnd increase the

//this is because we need one more loop ocurrrence

if (IsWeekEnd(fromDate))

else

//When businessDays is not equal to noOfDays,

//add one day in the current date.

{

}

else

{

break;

}

}

return fromDate;

}

This methos return the Business day’s date which comes after “N” business days.
We can use above method as follows:
Response.Write(FindBusinessDay(7, Convert.ToDateTime("6 / 13 / 2010")));

OUTPUT:
6/22/2010 12:00:00 AM

If you want to find out the the All business date which comes in “N” buseness days from given Date.
The return type of this method is Dictionary.

{

Dictionary<string, Enum> dictionary = new Dictionary<string, Enum>();

//This is used to count the number of business days

for (int i = 1; i <= numberOfBusinessDays; i++)

{

//if current date is the not WeekEnd increase the  businessDays with one

//as key and DayofWeek as value

//otherwise increase numberOfBusinessDays with one for one more loop occourance.

if (!IsWeekEnd(fromDate))

{

}

else

//When businessDays is not equal to noOfDays,

//add one day in the current date.

{

}

else

{

break;

}

}

return dictionary;

}

this method return all the Business days which fall in “N” business days and from given date, we store that all the business day in the dictionary.

We can use above method as follows:
suppose we want to find out which falls in 7 business days and from date  6/13/2010 .

{

}

OUTPUT:
6/14/2010 : Monday
6/15/2010 : Tuesday
6/16/2010 : Wednesday
6/17/2010 : Thursday

6/18/2010 : Friday
6/21/2010 : Monday
6/22/2010 : Tuesday

Attachments