I have got the following, but its not quite what I need now - It returns the dates of all the Fridays for the month passed in.
public static IEnumerable<DateTime> ReturnNextNthWeekdaysOfMonth(DateTime dt, DayOfWeek weekday, int amounttoshow = 4)
{
var days =
Enumerable.Range(1, DateTime.DaysInMonth(dt.Year, dt.Month)).Select(
day => new DateTime(dt.Year, dt.Month, day));
var weekdays = from day in days
where day.DayOfWeek == weekday
orderby day.Day ascending
select day;
return weekdays.Take(amounttoshow);
}
HOWEVER I now want to return the next Nth Fridays dates from todays date, irrelavant of the month they are in.
And I'm a bit stuck... Any help greatly appreciated.
public static IEnumerable<DateTime> ReturnNextNthWeekdaysOfMonth(DateTime dt, DayOfWeek weekday, int amounttoshow = 4)
{
// Find the first future occurance of the day.
while(dt.DayOfWeek != weekday)
dt = dt.AddDays(1);
// Create the entire range of dates required.
return Enumerable.Range(0, amounttoshow).Select(i => dt.AddDays(i * 7));
}
This first looks for the next day matching weekday
then proceeds to create amounttoshow
DateTime instances each of which is 7 days further than the previous, starting at the found date.