First, you'll have to sort your array, or at least make a sorted copy if it's important that you keep the order in the original list. If you don't, this isn't going to be easy.
Then, find the index of the element you're checking for. Once you've done that, create a loop going forwards, and one going backwards in your array, and count the date you've reached in the loop if the two dates are consective; otherwise, stop that loop (and do the other loop if you haven't done that already).
This assumes that all days in the range you're looking for can be found in the list, including weekends and holidays.
EDIT: If you want to include weekends implictly, you'll have to check the weekday using DayOfWeek while iterating over the days. The simplest way to do this is probably to also maintain a variable for the next expected day; every iteration in your loop, you add (or subtract, when moving backwards through the sorted array) one day. If the day you're processing is a Friday (or Monday when moving backwards), add/subtract an additional 2 days to skip the weekend.
Here's the basic idea (not tested, but you should get the point). I'm assuming that you've sorted dates
already, and that you've found the initial date in your array (at index i
). For simplicity, I'm also assuming that vacationDateToCheck
is never a Saturday or Sunday; if it can be either of those, you'll have to adjust accordingly.
DateTime expectedDate = vacationDateToCheck.AddDays(1);
if (vacationDateToCheck.DayOfWeek == DayOfWeek.Friday)
expectedDate = expectedDate.AddDays(2);
DateTime startDate = vacationDateToCheck;
DateTime endDate = vacationDateToCheck;
for (int j = i + 1; i < dates.Length; i++) {
if (dates[i] == expectedDate) {
endDate = dates[i];
expectedDate = dates[i].AddDays(1);
if (dates[i].DayOfWeek == DayOfWeek.Friday)
expectedDate = expectedDate.AddDays(2);
}
else
{
break;
}
}
Iterating the other way is similar, only you add -1 and -2 days instead, and you check if the day of the week is a Monday.