计算两个日期之间的工作日数? [英] Calculate the number of business days between two dates?

查看:182
本文介绍了计算两个日期之间的工作日数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我怎么能计算商家(或周日)的天数两个日期之间?

In C#, how can I calculate the number of business (or weekdays) days between two dates?

推荐答案

我有这样的任务之前,我已经得到了解决。
我会避免枚举所有天当它是可以避免的,在这里是如此的。我甚至不提创造了一堆的DateTime实例,正如我在上面的答案之一看到。这确实是处理能力的浪费。尤其是在现实世界的情况,当你有检查的几个月时间间隔。
看到我的code,有意见,下文。

I've had such a task before and I've got the solution. I would avoid enumerating all days in between when it's avoidable, which is the case here. I don't even mention creating a bunch of DateTime instances, as I saw in one of the answers above. This is really waste of processing power. Especially in the real world situation, when you have to examine time intervals of several months. See my code, with comments, below.

    /// <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月修改

伟大的答案!有小虫虽然。我拿的自由,因为回答者不存在自2009年来编辑这个答案。

Great answer! There is little bug though. I take the freedom to edit this answer since the answerer is absent since 2009.

上面的code假定 DayOfWeek.Sunday 值为 7 这是不是这样的。该值实际上是 0 。这导致了错误的计算,如果例如 firstDay LASTDAY 都是同一个星期天。该方法返回 1 在这种情况下,但它应该是 0

The code above assumes that DayOfWeek.Sunday has the value 7 which is not the case. The value is actually 0. It leads to a wrong calculation if for example firstDay and lastDay are both the same Sunday. The method returns 1 in this case but it should be 0.

此错误最简单的解决方法:更换在code其中 Firstdayofweek可 lastDayOfWeek 是线上方通过以下声明:

Easiest fix for this bug: Replace in the code above the lines where firstDayOfWeek and lastDayOfWeek are declared by the following:

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

这篇关于计算两个日期之间的工作日数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆