计算两个日期之间的工作日数?

计算两个日期之间的工作日数?,第1张

计算两个日期之间的工作日数?

我之前有过这样的任务,并且已经找到了解决方案。在这种情况下,我会避免列举所有两天之间可避免的情况。正如我在上面的答案之一中所看到的,我什至没有提到创建一堆DateTime实例。这确实是在浪费处理能力。特别是在现实世界中,当您必须检查几个月的时间间隔时。请在下面查看我的代码及其注释。

    /// <summary>    /// Calculates number of business days, taking into account:    ///  - weekends (Saturdays and Sundays)    ///  - bank holidays in the middle of the week    /// </summary>    /// <param name="firstDay">First day in the time interval</param>    /// <param name="lastDay">Last day in the time interval</param>    /// <param name="bankHolidays">List of bank holidays excluding weekends</param>    /// <returns>Number of business days during the 'span'</returns>    public static int BusinessDaysUntil(this DateTime firstDay, DateTime lastDay, params DateTime[] bankHolidays)    {        firstDay = firstDay.Date;        lastDay = lastDay.Date;        if (firstDay > lastDay) throw new ArgumentException("Incorrect last day " + lastDay);        TimeSpan span = lastDay - firstDay;        int businessDays = span.Days + 1;        int fullWeekCount = businessDays / 7;        // find out if there are weekends during the time exceedng the full weeks        if (businessDays > fullWeekCount*7)        { // we are here to find out if there is a 1-day or 2-days weekend // in the time interval remaining after subtracting the complete weeks int firstDayOfWeek = (int) firstDay.DayOfWeek; int lastDayOfWeek = (int) lastDay.DayOfWeek; if (lastDayOfWeek < firstDayOfWeek)     lastDayOfWeek += 7; if (firstDayOfWeek <= 6) {     if (lastDayOfWeek >= 7)// Both Saturday and Sunday are in the remaining time interval         businessDays -= 2;     else if (lastDayOfWeek >= 6)// only Saturday is in the remaining time interval         businessDays -= 1; } else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7)// only Sunday is in the remaining time interval     businessDays -= 1;        }        // subtract the weekends during the full weeks in the interval        businessDays -= fullWeekCount + fullWeekCount;        // subtract the number of bank holidays during the time interval        foreach (DateTime bankHoliday in bankHolidays)        { DateTime bh = bankHoliday.Date; if (firstDay <= bh && bh <= lastDay)     --businessDays;        }        return businessDays;    }

Slauma编辑,2011年8月

好答案!虽然没有什么bug。自2009年以来就没有应答者,因此我可以自由地编辑此答案。

上面的代码假定

DayOfWeek.Sunday
该值
7
不是这种情况。该值实际上是
0
。例如
firstDay
,如果
lastDay
两个星期天都相同,则会导致错误的计算。
1
在这种情况下,该方法返回,但应该为
0

此错误的最简单修复:在以下代码中声明

firstDayOfWeek
和的行上方的代码中替换
lastDayOfWeek
以下内容:

int firstDayOfWeek = firstDay.DayOfWeek == DayOfWeek.Sunday     ? 7 : (int)firstDay.DayOfWeek;int lastDayOfWeek = lastDay.DayOfWeek == DayOfWeek.Sunday    ? 7 : (int)lastDay.DayOfWeek;

现在的结果是:

  • 周五至周五-> 1
  • 周六至周六-> 0
  • 周日至周日-> 0
  • 周五至周六-> 1
  • 周五至周日-> 1
  • 星期五至星期一-> 2
  • 周六至周一-> 1
  • 周日至周一-> 1
  • 周一至周一-> 1


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5441167.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存