获取工作日DateTime两个日期的列表 [英] Get working days DateTime List of two dates
本文介绍了获取工作日DateTime两个日期的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取两个日期之间的工作日列表,但我只是一个月而已。
Im trying to get a List of working days between two dates but I got just from a month.
var workingDays = Enumerable.Range(1, daysInMonth)
.Where(d =>
!weekends.Contains(new DateTime(last30Days.Year, last30Days.Month, d).DayOfWeek)).ToList();
但是这样一来,我只有一个特定的月份。
But this way i get just one speciefic month.
推荐答案
从函数开始,获取两个日期之间的所有天数:
Start out with a function to get all days between two dates:
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);
}
}
然后只过滤掉非工作日:
Then just filter out the non-working days:
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;
}
这篇关于获取工作日DateTime两个日期的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文