Why is this date not the week of next year? (C # calendar)

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 the week is Monday. I want to use the four-day rule, which means that the week belongs to the year in which most of its days lie. December 31, 2001 - Monday, and then - on the 1st week of 2002. However, weekOfYear returns as 53.

Does anyone know what is wrong?

+4
source share
1 answer

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

+4
source

Source: https://habr.com/ru/post/1393983/


All Articles