Loading ...

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

Calculate Business days in ASP.NET C#

 /5
0 (0votes)

Download Sample Project

 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.

public static DateTime FindBusinessDay(int numberOfBusinessDays, DateTime fromDate)

    {

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

        int businessDays = 0;

        int noOfDays = numberOfBusinessDays;

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

        {

            //if current date is the WeekEnd increase the
            //numberOfBusinessDays with one

            //this is because we need one more loop ocurrrence

            if (IsWeekEnd(fromDate))

                numberOfBusinessDays++;

            else

                businessDays++;

 

            //When businessDays is not equal to noOfDays,

            //add one day in the current date.

            if (businessDays != noOfDays)

            {

                fromDate = fromDate.AddDays(1);

            }

            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.

 

public static Dictionary<string, Enum> FindBusinessDays(int numberOfBusinessDays, DateTime fromDate)

    {

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

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

        int businessDays = 0;

        int noOfDays = numberOfBusinessDays;

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

        {

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

            //And add that business day in the Dictionary with fromdate
            //as key and DayofWeek as value

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

            if (!IsWeekEnd(fromDate))

            {

                businessDays++;

                dictionary.Add(fromDate.ToShortDateString(), fromDate.DayOfWeek);

            }

            else

                numberOfBusinessDays++;

 

            //When businessDays is not equal to noOfDays,

            //add one day in the current date.

            if (businessDays != noOfDays)

            {

                fromDate = fromDate.AddDays(1);

            }

            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 .

StringBuilder sbBusinessDays = new StringBuilder();

foreach (var businessDay in BusinessDays.FindBusinessDays(7, Convert.ToDateTime("6 / 13 / 2010")))

 {

   sbBusinessDays.AppendFormat("{0} : {1} <br/>", businessDay.Key, businessDay.Value);

 }

 Response.Write(sbBusinessDays.ToString());

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

Comments (no comments yet)

  • :*
  • :*
  • :
 *

Top Posts