Calculate the number of business days between two dates?

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

    Here is the function which we can use to calculate business days between two date. I'm not using holiday list as it can vary accross country/region.

    If we want to use it anyway we can take third argument as list of holiday and before incrementing count we should check that list does not contains d

    public static int GetBussinessDaysBetweenTwoDates(DateTime StartDate,   DateTime EndDate)
        {
            if (StartDate > EndDate)
                return -1;
    
            int bd = 0;
    
            for (DateTime d = StartDate; d < EndDate; d = d.AddDays(1))
            {
                if (d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday)
                    bd++;
            }
    
            return bd;
        }
    

提交回复
热议问题