Calculate the number of business days between two dates?

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

    Here is an approach if you are using MVC. I have also calculated national holidays or any festive days to be excluded by fetching it from holidayscalendar which you will need to make one.

            foreach (DateTime day in EachDay(model))
            {
                bool key = false;
                foreach (LeaveModel ln in holidaycalendar)
                {
                    if (day.Date == ln.Date && day.DayOfWeek != DayOfWeek.Saturday && day.DayOfWeek != DayOfWeek.Sunday)
                    {
                        key = true; break;
                    }
                }
                if (day.DayOfWeek == DayOfWeek.Saturday || day.DayOfWeek == DayOfWeek.Sunday)
                {
                    key = true;
                }
                if (key != true)
                {
                    leavecount++;
                }
            }
    

    Leavemodel is a list here

提交回复
热议问题