Monday, September 5, 2011

Business day calculation in C#

Here are some simple Csharp functions to find out the first business day, second business day, third business day and last business day for any given month and year.

Here we are assuming our working days to be from Monday to Friday and we are not considering any holidays (bank holidays, national holidays, etc.).

Function to get first business day of the month and year provided.

  public DateTime GetFirstBusinessDay(int Year, int Month)
        {
            DateTime FirstOfMonth = default(DateTime);
            DateTime FirstBusinessDay = default(DateTime);
            FirstOfMonth = new DateTime(Year, Month, 1);
            if (FirstOfMonth.DayOfWeek == DayOfWeek.Sunday)
            {
                FirstBusinessDay = FirstOfMonth.AddDays(1);
            }
            else if (FirstOfMonth.DayOfWeek == DayOfWeek.Saturday)
            {
                FirstBusinessDay = FirstOfMonth.AddDays(2);
            }
            else
            {
                FirstBusinessDay = FirstOfMonth;
            }
            return FirstBusinessDay;
        }

Function to get second business day of the month and year provided.

public DateTime GetSecondBusinessDay(int Year, int Month)
        {
            DateTime FirstOfMonth = default(DateTime);
            DateTime SecondBusinessDay = default(DateTime);
            FirstOfMonth = new DateTime(Year, Month, 1);
            if (FirstOfMonth.DayOfWeek == DayOfWeek.Sunday)
            {
                SecondBusinessDay = FirstOfMonth.AddDays(2);
            }
            else if (FirstOfMonth.DayOfWeek == DayOfWeek.Saturday)
            {
                SecondBusinessDay = FirstOfMonth.AddDays(3);
            }
            else if (FirstOfMonth.DayOfWeek == DayOfWeek.Friday)
            {
                SecondBusinessDay = FirstOfMonth.AddDays(3);
            }
            else
            {
                SecondBusinessDay = FirstOfMonth.AddDays(1);
            }
           
            return SecondBusinessDay;
        }


Function to get third business day of the month and year provided.

public DateTime GetThirdBusinessDay(int Year, int Month)
        {
            DateTime FirstOfMonth = default(DateTime);
            DateTime ThirdBusinessDay = default(DateTime);
            FirstOfMonth = new DateTime(Year, Month, 1);
            if (FirstOfMonth.DayOfWeek == DayOfWeek.Sunday)
            {
                ThirdBusinessDay = FirstOfMonth.AddDays(3);
            }
            else if (FirstOfMonth.DayOfWeek == DayOfWeek.Saturday)
            {
                ThirdBusinessDay = FirstOfMonth.AddDays(4);
            }
            else if (FirstOfMonth.DayOfWeek == DayOfWeek.Friday)
            {
                ThirdBusinessDay = FirstOfMonth.AddDays(4);
            }
            else if (FirstOfMonth.DayOfWeek == DayOfWeek.Thursday)
            {
                ThirdBusinessDay = FirstOfMonth.AddDays(4);
            }
            else
            {
                ThirdBusinessDay = FirstOfMonth.AddDays(2);
            }

            return ThirdBusinessDay;
        }
 
Function to get last business day of the month and year provided.

public DateTime GetLastBusinessDay(int Year, int Month)
        {
            DateTime LastOfMonth = default(DateTime);
            DateTime LastBusinessDay = default(DateTime);
            LastOfMonth = new DateTime(Year, Month, DateTime.DaysInMonth(Year, Month));
            if (LastOfMonth.DayOfWeek == DayOfWeek.Sunday)
            {
                LastBusinessDay = LastOfMonth.AddDays(-2);
            }
            else if (LastOfMonth.DayOfWeek == DayOfWeek.Saturday)
            {
                LastBusinessDay = LastOfMonth.AddDays(-1);
            }
            else
            {
                LastBusinessDay = LastOfMonth;
            }
            return LastBusinessDay;
        }

1 comment: