Calculate the number of business days between two dates?

后端 未结 30 1840
悲&欢浪女
悲&欢浪女 2020-11-22 14:54

In C#, how can I calculate the number of business (or weekdays) days between two dates?

30条回答
  •  天命终不由人
    2020-11-22 15:41

    This is a generic solution.

    startdayvalue is day number of start date.

    weekendday_1 is day numner of week end.

    day number - MON - 1, TUE - 2, ... SAT - 6, SUN -7.

    difference is difference between two dates..

    Example : Start Date : 4 April, 2013, End Date : 14 April, 2013

    Difference : 10, startdayvalue : 4, weekendday_1 : 7 (if SUNDAY is a weekend for you.)

    This will give you number of holidays.

    No of business day = (Difference + 1) - holiday1

        if (startdayvalue > weekendday_1)
        {
    
            if (difference > ((7 - startdayvalue) + weekendday_1))
            {
                holiday1 = (difference - ((7 - startdayvalue) + weekendday_1)) / 7;
                holiday1 = holiday1 + 1;
            }
            else
            {
                holiday1 = 0;
            }
        }
        else if (startdayvalue < weekendday_1)
        {
    
            if (difference > (weekendday_1 - startdayvalue))
            {
                holiday1 = (difference - (weekendday_1 - startdayvalue)) / 7;
                holiday1 = holiday1 + 1;
            }
            else if (difference == (weekendday_1 - startdayvalue))
            {
                holiday1 = 1;
            }
            else
            {
                holiday1 = 0;
            }
        }
        else
        {
            holiday1 = difference / 7;
            holiday1 = holiday1 + 1;
        }
    

提交回复
热议问题