从获取两个日期之间的所有天数的函数开始:
public static IEnumerable<DateTime> DaysBetween(DateTime start, DateTime end)
{
var current = start;
if (current != current.Date) //handle the case where the date isn't already midnight
current = current.AddDays(1).Date;
while (current < end)
{
yield return current;
current = current.AddDays(1);
}
}
然后只需过滤掉非工作日:
public static IEnumerable<DateTime> WorkDayBetween(DateTime start, DateTime end)
{
return DaysBetween(start, end)
.Where(date => IsWorkDay(date));
}
//feel free to use alternate logic here, or to account for holidays, etc.
private static bool IsWorksDay(DateTime date)
{
return date.DayOfWeek != DayOfWeek.Saturday
&& date.DayOfWeek != DayOfWeek.Sunday;
}