计数一周中工作的天数,不循环



我需要计算工作的总天数,给定结构,说明一周中的哪些天是工作的,以及从和到日期。

我目前的算法是:

protected int TotalWorkDays(DateTime From, DateTime dtTo,WorkedDays days)
    {
        int Days = 0;
        DayOfWeek DOW;
        for (DateTime curDate = From; curDate.Date <= dtTo.Date; curDate = curDate.AddDays(1))
        {
            DOW = curDate.DayOfWeek;
            if ((DOW == DayOfWeek.Sunday &  days.Sunday) |
                (DOW == DayOfWeek.Monday &  days.Monday) |
                (DOW == DayOfWeek.Tuesday &  days.Tuesday) |
                (DOW == DayOfWeek.Wednesday &  days.Wednesday) |
                (DOW == DayOfWeek.Thursday &  days.Thursday) |
                (DOW == DayOfWeek.Friday &  days.Friday) |
                (DOW == DayOfWeek.Saturday &  days.Saturday)
               )
            {
                Days += 1;
            }
        }
        return Days;
    }

我几乎肯定这可以在没有循环的情况下完成,但我似乎不明白。有人能帮我找到一个更有效的算法吗?

查找开始日期和结束日期之间的周数(使用减法和除法)。然后乘以每周工作的天数。对结束情况做一些减法(开始/结束日期在一周的中间)。

hmmmm。。。。

创建一个从DayOfWeek(如果我没记错的话是int)到bool的字典,然后。。。。

var DaysWorked = (from dayoffset in Enumerable.Range(0, (To - From).TotalDays)
                  where WorkingDays[From.AddDays(dayoffset).DayOfWeek]
                  select dayoffset).Count();

但并不完全有效!

看看这篇代码项目文章,它解释了如何在不循环的情况下完成它;)

编辑:以下是它使用的公式:

  1. 以周为单位计算时间跨度的数量。可以这么说,W
  2. 从周数中减去第一周。W=W-1
  3. 将周数乘以每周的工作天数。称之为D
  4. 找出指定时间段内的假期。叫它吧,H
  5. 计算第一周的天数。叫它吧,SD
  6. 计算上周的天数。叫它吧,ED
  7. 把所有的日子加起来。BD=D+SD+ED�H

您可以利用一周中每七天重复一次的事实。以下是一个算法的基本概述:

  1. 计算第一个部分周的工作天数
  2. 计算上一部分周的工作天数
  3. 计算中间的整周数,然后乘以一周的工作天数
  4. 将以上三个值相加
var workDays = new DayOfWeek[]{ DayOfWeek.Monday, DayOfWeek.Tuesday};
var days = TotalWorkDays(new DateTime(2005,1,12), new DateTime(2005,3,15), workDays);
protected int TotalWorkDays(DateTime start, DateTime end, DayOfWeek[] workDays)
{
    var weeks = (int)Math.Floor((end - start).TotalDays / 7); 
    var days = weeks * workDays.Length;
    //Calc rest
    var d = start.AddDays(weeks * 7);
    while (d <= end)
    {
        if(workDays.Contains(d.DayOfWeek)) 
            days++;
        d = d.AddDays(1);
    }   
    return days;
}

您可以使用以下算法:

  • 计算开始一周的工作日(最多7次迭代)
  • 计算开始/结束之间的周数以及工作日的周数
  • 计算最后一周的工作日(最多7次迭代)

该示例使用的时间段库的类WeekDateDiff。净

// ----------------------------------------------------------------------
public int CountWorkingDays( DateTime start, DateTime end, IList<DayOfWeek> workingDays )
{
  if ( workingDays.Count == 0 )
  {
    return 0;
  }
  Week startWeek = new Week( start );
  Week endWeek = new Week( end );
  int dayCount = 0;
  // start week
  DateTime currentDay = start.Date;
  while ( currentDay < startWeek.End )
  {
    if ( workingDays.Contains( currentDay.DayOfWeek ) )
    {
      dayCount++;
    }
    currentDay = currentDay.AddDays( 1 );
  }
  // between weeks
  DateDiff inBetweenWeekDiff = new DateDiff( startWeek.End, endWeek.Start );
  dayCount += inBetweenWeekDiff.Weeks * workingDays.Count;
  // end week
  currentDay = endWeek.Start.Date;
  while ( currentDay < end )
  {
    if ( workingDays.Contains( currentDay.DayOfWeek ) )
    {
      dayCount++;
    }
    currentDay = currentDay.AddDays( 1 );
  }
  return dayCount;
} // CountWorkingDays

用法:

// ----------------------------------------------------------------------
public void CountWorkingDaysSample()
{
  DayOfWeek[] workingDays = new [] { DayOfWeek.Monday, DayOfWeek.Tuesday };
  DateTime start = new DateTime( 2011, 3, 1 );
  DateTime end = new DateTime( 2011, 5, 1 );
  Console.WriteLine( "working days: {0}", CountWorkingDays( start, end, workingDays ) );
  // > working days: 19
} // CountWorkingDaysSample

相关内容

  • 没有找到相关文章

最新更新