Below code calculates the number of workdays to be added and if the end date falls on holidays/weekends, shift the date to the next day.
But this code is on an assumption that the start date is not on weekends/holidays.
I want the code which also works if the start date falls on a weekend/holiday.
Please note this code was posted by ElenaSofea on 17 Jun'13 but as I was not able to comment on it asking this as a new question.
Reference: How to add a number of days to a Date while skipping weekends and other 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);
numberofWorkDays++;
}
else
{
futureDate = futureDate.AddDays(1);
}
}
while(futureDate.DayOfWeek == DayOfWeek.Saturday
|| futureDate.DayOfWeek == DayOfWeek.Sunday
|| (holidays != null && holidays.Contains(futureDate)))
{
futureDate = futureDate.AddDays(1);
}
return futureDate;
}