4

I want to calculate the week number of a given date.

DateTime date = Convert.ToDateTime("31.12.2001");

DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
System.Globalization.Calendar cal = dfi.Calendar;

int weekOfYear = cal.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek,  
   dfi.FirstDayOfWeek);

In my DateTimeFormatInfo, the first day of week is a monday. I want to use the four days rule, means a week belongs to a year in which the majority of its days lie. The 31st of December 2001 is a monday and should then be in week 1 of 2002. However, weekOfYear is returned as 53.

Does someone know what´s wrong?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120

1 Answers1

4

See the documentation for CalendarWeekRule for the exact rules. In short, December 31 will never be week 1, it will be always something like 52/53. The values on the enumeration influence what week will the first days of January have.

svick
  • 236,525
  • 50
  • 385
  • 514