Introduction
The solution below is based on this tip by John Simmons and my alternative suggestions in its comments section.
The Code
public static List<DateTime> GetDatesByOrdinalDayV2(
DateTime startDate,
DateTime endDate,
DayOfWeek dayOfWeek,
int ordinal)
{
var foundDates = new List<DateTime>();
ordinal = Math.Min(Math.Max(ordinal, 1), 5);
while (startDate < endDate)
{
int month = startDate.Month;
var workingDate = new DateTime(startDate.Year, month, 1);
workingDate = (int)workingDate.DayOfWeek > (int)dayOfWeek
? workingDate.AddDays(7 - (int)workingDate.DayOfWeek + (int)dayOfWeek)
: workingDate.AddDays((int)dayOfWeek - (int)workingDate.DayOfWeek);
workingDate = workingDate.AddDays((ordinal - 1) * 7);
workingDate = workingDate.Month != month ? workingDate.AddDays(-7) : workingDate;
foundDates.Add(workingDate);
startDate = startDate.AddMonths(1);
}
return foundDates;
}
Using the Code
Set the ordinal parameter to the occurrence in the month for the day of the week you require. So it's set to 1 for the first, 2 for the second, etc. If you enter 5 and there are not 5 occurrences in a particular month, you will get the date of the last occurrence in that month. Here's an example that returns the date of every second Friday of the month in 2016.
private static void Main()
{
DateTime beg = DateTime.Parse("01/01/2016");
DateTime end = DateTime.Parse("01/01/2017");
List<DateTime> SecondFridays = GetDatesByOrdinalDayV2(beg, end, DayOfWeek.Friday, 2);
var dateStrings = SecondFridays
.Select(dateTime => dateTime.ToString("MMMM dd, yyyy")).ToList();
File.WriteAllLines(@"C:\Temp\DateTest.txt", dateStrings);
}