I'm assuming you want to find the end date for something given a start date and the number of working days that it will take?
If so, this the following code may be what you want. It assumes that you have a list of all the holidays for the maximum range of time that you are supporting. That could be a few years in the future, depending on your requirements!
This code takes the start date and the number of working days (and the list of holidays) and returns the end date.
public static DateTime AddWorkingDays(DateTime start, int workingDays, IEnumerable<DateTime> holidays)
{
var dict = new HashSet<DateTime>(holidays);
DateTime date;
for (date = start; workingDays > 0; date = date.AddDays(1))
{
if (!dict.Contains(date))
{
--workingDays;
}
}
return date;
}
An alternative implementation assumes that you have a predicate you can call to determine if a day is a holiday:
public static DateTime AddWorkingDays(DateTime start, int workingDays, Predicate<DateTime> isHoliday)
{
DateTime date;
for (date = start; workingDays > 0; date = date.AddDays(1))
{
if (!isHoliday(date))
{
--workingDays;
}
}
return date;
}
Both of these implementations suffer from visiting every date. This is possibly not an issue, but if the date range is large it could be too slow.