Counting regular working days in a given period of time

后端 未结 8 431
庸人自扰
庸人自扰 2021-01-12 05:38

need some help. I need to count regular working days for a given date period, for example, in our country, we have 5 regular working days monday to friday, then in code i ne

8条回答
  •  失恋的感觉
    2021-01-12 06:07

    You could try a simple method of just counting the days. If this is usually done for periods of time like months and not years and isn't called repeatedly then this will not be a performance hit to just walk it.

    int GetWorkingDays(DateTime startDate, DateTime endDate)
    {
        int count = 0;
        for (DateTime currentDate = startDate; currentDate < endDate; currentDate = currentDate.AddDays(1))
        {
            if (currentDate.DayOfWeek == DayOfWeek.Sunday || currentDate.DayOfWeek == DayOfWeek.Saturday)
            {
                continue;
            }
            count++;
        }
        return count;
    }
    

提交回复
热议问题