I am struggling to find an easy and efficient solution to calculating the week day of a month. For example, if a given date is the first Monday Monday 5th March 2018
then I want to get the date for each first Monday of a month for the next 6 months e.g: Monday 2nd April 2018
and Monday 3rd May 2018
and so on.
I have tried to use the following code from this question. However, the code below only returns the week number I would like it to return the whole date.
static class DateTimeExtensions
{
static GregorianCalendar _gc = new GregorianCalendar();
public static int GetWeekOfMonth(this DateTime time)
{
DateTime first = new DateTime(time.Year, time.Month, 1);
return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
}
static int GetWeekOfYear(this DateTime time)
{
return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
}
}
But I am confused and don't know how I can modify the above code to solve this issue. Any help will be appreciated.