The solution to find out how many Sundays, Mondays etc. in the month.
private int GetOccuranceOfWeekday(int Year, int Month, DayOfWeek Weekday)
{
int ReturnValue = 0;
DateTime MyDate = new DateTime(Year, Month, 1);
int Start = 1;
if (Weekday != MyDate.DayOfWeek)
{
Start = -(MyDate.DayOfWeek - Weekday - 1);
if (Start <= 0)
{
ReturnValue = -1;
}
}
while (Start <= DateTime.DaysInMonth(Year, Month))
{
ReturnValue += 1;
Start += 7;
}
return ReturnValue;
}
Just call it like.
MessageBox.Show(string.Format("There are {0} Saturdays in September 2013", GetOccuranceOfWeekday(2013, 9, DayOfWeek.Saturday)));
This May Help.