计算在给定时间段内的正常工作日

时间:2010-09-14 12:34:52

标签: c# algorithm datetime

需要一些帮助。我需要计算一个特定日期的正常工作日,例如,在我们国家,我们星期一到星期五有5个正常工作日,然后在代码中我需要排除星期六和星期日,当我在我的计算中使用它时。

我需要在C#中使用类似的算法:

    int GetRegularWorkingDays(DateTime startDate, DateTime endDate)
    {

       int nonWorkingDays = ((endDate - startDate) % 7) * 2;

       return (endDate - startDate) - nonWorkingDays;
    }

我知道我的选秀很开心:(。提前致谢。=)

PS:请大家投票选出下面最好/最快/最有效的答案。谢谢=)

8 个答案:

答案 0 :(得分:18)

查看this example on Code Project使用非常有效的方法,不涉及任何循环;)

它使用了这个算法:

  
      
  1. 以周为单位计算时间跨度。叫它,W。
  2.   
  3. 从周数中扣除第一周。 W = W-1
  4.   
  5. 将周数乘以每个工作日的天数   周。叫它,D。
  6.   
  7. 在指定的时间范围内查找假期。叫它,H。
  8.   
  9. 计算第一周的天数。叫它,SD。
  10.   
  11. 计算上周的天数。叫它,ED。
  12.   
  13. 总结所有的日子。 BD = D + SD +ED H。
  14.   

答案 1 :(得分:4)

一衬垫!

int workingDays = Enumerable.Range(0, Convert.ToInt32(endDate.Subtract(startDate).TotalDays)).Select(i=>new [] { DayOfWeek.Saturday, DayOfWeek.Sunday }.Contains(startDate.AddDays(i).DayOfWeek) ? 0 : 1).Sum();

或更高效:

DayOfWeek currDay = startDate.DayOfWeek;
int nonWorkingDays = 0;

foreach(var i in Enumerable.Range(0, Convert.ToInt32(endDate.Subtract(startDate).TotalDays)))
{
     if(currDay == DayOfWeek.Sunday || currDay == DayOfWeek.Saturday) 
          nonWorkingDays++;
     if((int)++currDay > 6) currDay = (DayOfWeek)0;
}

答案 2 :(得分:4)

我写了一个类型扩展器,允许我在给定日期添加(或减去)工作日。也许这会对你有所帮助。效果很好,所以如果这对你有所帮助,请投票给我的帖子。

    /// <summary>
    /// Adds weekdays to date
    /// </summary>
    /// <param name="value">DateTime to add to</param>
    /// <param name="weekdays">Number of weekdays to add</param>
    /// <returns>DateTime</returns>
    public static DateTime AddWeekdays(this DateTime value, int weekdays)
    {
        int direction = Math.Sign(weekdays);
        int initialDayOfWeek = Convert.ToInt32(value.DayOfWeek);

        //---------------------------------------------------------------------------
        // if the day is a weekend, shift to the next weekday before calculating
        if ((value.DayOfWeek == DayOfWeek.Sunday && direction < 0)
            || (value.DayOfWeek == DayOfWeek.Saturday && direction > 0))
        {
            value = value.AddDays(direction * 2);
            weekdays += (direction * -1); // adjust days to add by one
        }
        else if ((value.DayOfWeek == DayOfWeek.Sunday && direction > 0)
            || (value.DayOfWeek == DayOfWeek.Saturday && direction < 0))
        {
            value = value.AddDays(direction);
            weekdays += (direction * -1); // adjust days to add by one
        }
        //---------------------------------------------------------------------------

        int weeksBase = Math.Abs(weekdays / 5);
        int addDays = Math.Abs(weekdays % 5);

        int totalDays = (weeksBase * 7) + addDays;
        DateTime result = value.AddDays(totalDays * direction);

        //---------------------------------------------------------------------------
        // if the result is a weekend, shift to the next weekday
        if ((result.DayOfWeek == DayOfWeek.Sunday && direction > 0)
            || (result.DayOfWeek == DayOfWeek.Saturday && direction < 0))
        {
            result = result.AddDays(direction);
        }
        else if ((result.DayOfWeek == DayOfWeek.Sunday && direction < 0)
            || (result.DayOfWeek == DayOfWeek.Saturday && direction > 0))
        {
            result = result.AddDays(direction * 2);
        }
        //---------------------------------------------------------------------------

        return result;
    }

答案 3 :(得分:2)

您可以尝试一种简单的计算天数的方法。如果这通常是在几个月而不是几年的时间段内完成的,并且不会被重复调用,那么这不会仅仅是一次性能打击。

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;
}

答案 4 :(得分:2)

速度不是很快,但这样做可以解决问题:

int GetRegularWorkingDays(DateTime start, DateTime end)
{
    return (
        from day in Range(start, end)
        where day.DayOfWeek != DayOfWeek.Saturday
        where day.DayOfWeek != DayOfWeek.Sunday
        select day).Count();
}

IEnumerable<DateTime> Range(DateTime start, DateTime end)
{
    while (start <= end)
    {
        yield return start;
        start = start.AddDays(1);
    }
}

答案 5 :(得分:1)

你可以用时间线辅助类来做 - 这个类也允许其他间隔:

public class TimeLine
{
    public static IEnumerable<DateTime> CreateTimeLine(DateTime start, TimeSpan interval) {
        return TimeLine.CreateTimeLine(start, interval, DateTime.MaxValue);
    }

    public static IEnumerable<DateTime> CreateTimeLine(DateTime start, TimeSpan interval, DateTime end) {
        var currentVal = start;
        var endVal = end.Subtract(interval);

        do {
            currentVal = currentVal.Add(interval);
            yield return currentVal;
        } while (currentVal <= endVal);
    }
}

要解决您的问题,您可以执行以下操作:

var workingDays = TimeLine.CreateTimeLine(DateTime.Now.Date, TimeSpan.FromDays(1), DateTime.Now.Date.AddDays(30))
                          .Where(x => x.DayOfWeek != DayOfWeek.Saturday && x.DayOfWeek != DayOfWeek.Sunday);
var noOfWorkingDays = workingDays.Count();

此时间线类可用于任何间隔的任何连续时间线。

答案 6 :(得分:0)

获得工作日的简单方法:

int GetRegularWorkingDays(DateTime startDate, DateTime endDate)
{
    int total = 0;

    if (startDate < endDate)
    {
        var days = endDate - startDate;

        for( ; startDate < endDate; startDate = startDate.AddDays(1) )
        {
            switch(startDate.DayOfWeek)
            {
                case DayOfWeek.Saturday :
                case DayOfWeek.Sunday :                     
                    break;
                default:
                    total++;
                    break;
            }
        }
    }
    return total;
}

答案 7 :(得分:0)

int count = 0;
switch (dateTimePicker2.Value.DayOfWeek.ToString())
{
    case "Saturday": count--; break;
    case "Sunday": count--; break;
    default:break;
}
switch (dateTimePicker3.Value.DayOfWeek.ToString())
{
    case "Saturday": count--; break;
    case "Sunday": count--; break;
    default:break;
}
if (count == -2)
    count = -1;
int weeks = t.Days / 7;
int daycount =count+ t.Days - (2 * weeks)+1;
label7.Text = "No of Days : " + daycount.ToString();