I am trying to find the number of weekday of month by passing a date in C#.
For ex.: If I pass "3/6/2013 (6-Mar-2013)" the function should say 1st wednesday of month. If I pass "3/1/2013 (1-Mar-2013)" the function should say 1st friday of month. If I pass "3/13/2013 (13-Mar-2013)" the function should say 2nd wednesday of month.
I was trying to use this function, but its returning the week of month(i.e. passing "3/6/2013 (6-Mar-2013)" returns 2):
public static int GetWeekOfMonth(DateTime date)
{
DateTime beginningOfMonth = new DateTime(date.Year, date.Month, 1);
while (date.Date.AddDays(1).DayOfWeek != CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)
date = date.AddDays(1);
return (int)Math.Truncate((double)date.Subtract(beginningOfMonth).TotalDays / 7f) + 1;
}
Please suggest on how to get the number of weekday of month.