【发布时间】:2011-12-28 08:33:16
【问题描述】:
给定一个日期。如何在跳过范围之间的周末和其他假期的同时添加天数?
List <DateTime> holidays = new List<DateTime>()
{
new DateTime(2012, 01, 03),
new DateTime(2012, 01, 26)
};
dateTimeReview.Value = CalculateFutureDate(dateTimeStart.Value, 7,holidays);
static DateTime CalculateFutureDate(DateTime fromDate, int numberofWorkDays, ICollection<DateTime> holidays)
{
var futureDate = fromDate;
for (var i = 0; i < numberofWorkDays; i++ )
{
if (futureDate.DayOfWeek == DayOfWeek.Saturday
|| futureDate.DayOfWeek == DayOfWeek.Sunday
|| (holidays != null && holidays.Contains(futureDate)))
{
futureDate = futureDate.AddDays(1); // Increase FutureDate by one because of condition
futureDate = futureDate.AddDays(1); // Add a working day
}
}
return futureDate;
}
【问题讨论】:
-
问题是..?你尝试了什么?
-
@user978511:我试过那个。但是我怎么能排除其他假期???
-
那么你为什么不写下你尝试过的东西以及为什么它对你不起作用?
-
给定的代码仅适用于周末...