i am getting week no from database need to calculate which week of the month it is.
for ex:
week no = 7 month = 2
week no 11 month = 3
How to calculate it??
Just to add on i also have the year value with the week no.
Ok, I'll have a go, but it's not very pretty:
public int MonthOfWeek( int week, int year, CalendarWeekRule, weekrule, DayOfWeek firstDayOfWeek)
{
GregorianCalendar gc = new GregorianCalendar();
for( DateTime dt = new DateTime(year, 1, 1); dt.Year == year; dt = dt.AddDays(1))
{
if( gc.GetWeekOfYear( dt, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) == week ){
return dt.Month;
}
}
return -1;
}
This takes into consideration the culture specific rules for generating weeks. If a week is split between two months it will return the first month it encounters.
So in Norway, I would call this with CalendarWeekRule.FirstFourDayWeek
and DayOfWeek.Monday
for correct results.