Below function will return you the list of dates.
CS:
private List<DateTime> GetDates(DayOfWeek dayOfWeek, int month, int year)
{
List<DateTime> dates = new List<DateTime>();
int totalDaysCount = DateTime.DaysInMonth(year,month);
int dayCount = 0;
do
{
DateTime currentDate = new DateTime(year, month, ++dayCount);
if (currentDate.DayOfWeek == dayOfWeek)
dates.Add(currentDate);
} while (totalDaysCount > dayCount);
return dates;
}
VB:
Public Function GetDates(ByVal dayOfWeek As DayOfWeek, ByVal month As Integer, ByVal year As Integer) As List(Of DateTime)
Dim dates As New List(Of DateTime)
Dim totalDaysCount As Integer = DateTime.DaysInMonth(year, month)
Dim dayCount As Integer = 0
Do While (totalDaysCount > dayCount)
dayCount += 1
Dim currentDate As New DateTime(year, month, dayCount)
If (currentDate.DayOfWeek = dayOfWeek) Then
dates.Add(currentDate)
End If
Loop
Return dates
End Function
Call wherever you wanted like below,
List<DateTime> dates = GetDates(DayOfWeek.Sunday, 1, 2011);
List<DateTime> dates = GetDates(DayOfWeek.Saturday, 4, 2011);
List<DateTime> dates = GetDates(DayOfWeek.Friday, 11, 2009);
The "
dates" will give all the Sundays in January, 2011.
Mark it as Answer if it helps you.