Calculate the number of business days between two dates?

后端 未结 30 1190
悲&欢浪女
悲&欢浪女 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:36

    I just improved @Alexander and @Slauma answer to support a business week as a parameter, for cases where saturday is a business day, or even cases where there is just a couple of days of the week that are considered business days:

    /// 
    /// Calculate the number of business days between two dates, considering:
    ///  - Days of the week that are not considered business days.
    ///  - Holidays between these two dates.
    /// 
    /// First day of the desired 'span'.
    /// Last day of the desired 'span'.
    /// Days of the week that are considered to be business days, if NULL considers monday, tuesday, wednesday, thursday and friday as business days of the week.
    /// Holidays, if NULL, considers no holiday.
    /// Number of business days during the 'span'
    public static int BusinessDaysUntil(this DateTime fDay, DateTime lDay, DayOfWeek[] BusinessDaysOfWeek = null, DateTime[] Holidays = null)
    {
        if (BusinessDaysOfWeek == null)
            BusinessDaysOfWeek = new DayOfWeek[] { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday };
        if (Holidays == null)
            Holidays = new DateTime[] { };
    
        fDay = fDay.Date;
        lDay = lDay.Date;
    
        if (fDay > lDay)
            throw new ArgumentException("Incorrect last day " + lDay);
    
        int bDays = (lDay - fDay).Days + 1;
        int fullWeekCount = bDays / 7;
        int fullWeekCountMult = 7 - WeekDays.Length;
        //  Find out if there are weekends during the time exceedng the full weeks
        if (bDays > (fullWeekCount * 7))
        {
            int fDayOfWeek = (int)fDay.DayOfWeek;
            int lDayOfWeek = (int)lDay.DayOfWeek;
    
            if (fDayOfWeek > lDayOfWeek)
                lDayOfWeek += 7;
    
            // If they are the same, we already covered it right before the Holiday subtraction
            if (lDayOfWeek != fDayOfWeek)
            {
                //  Here we need to see if any of the days between are considered business days
                for (int i = fDayOfWeek; i <= lDayOfWeek; i++)
                    if (!WeekDays.Contains((DayOfWeek)(i > 6 ? i - 7 : i)))
                        bDays -= 1;
            }
        }
    
        //  Subtract the days that are not in WeekDays[] during the full weeks in the interval
        bDays -= (fullWeekCount * fullWeekCountMult);
        //  Subtract the number of bank holidays during the time interval
        bDays = bDays - Holidays.Select(x => x.Date).Count(x => fDay <= x && x <= lDay);
    
        return bDays;
    }
    

提交回复
热议问题