我需要计算工作的总天数,给定结构,说明一周中的哪些天是工作的,以及从和到日期。
我目前的算法是:
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();
但并不完全有效!
看看这篇代码项目文章,它解释了如何在不循环的情况下完成它;)
编辑:以下是它使用的公式:
- 以周为单位计算时间跨度的数量。可以这么说,W
- 从周数中减去第一周。W=W-1
- 将周数乘以每周的工作天数。称之为D
- 找出指定时间段内的假期。叫它吧,H
- 计算第一周的天数。叫它吧,SD
- 计算上周的天数。叫它吧,ED
- 把所有的日子加起来。BD=D+SD+ED�H
您可以利用一周中每七天重复一次的事实。以下是一个算法的基本概述:
- 计算第一个部分周的工作天数
- 计算上一部分周的工作天数
- 计算中间的整周数,然后乘以一周的工作天数
- 将以上三个值相加
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次迭代)
该示例使用的时间段库的类Week和DateDiff。净
// ----------------------------------------------------------------------
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