Get or convert week to week to ISO

When trying to get the number of weeks per year, I tried this:

maxWeek = calendar.GetWeekOfYear(New Date(t_year, 12, 31), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) 

Which is bad for my purpura, but I noticed something strange. During 2014-12-31 he returned 53, and for 2015-01-01 he returned 1. On 2015-01-05 he returned 2. This means that weeks 53 and 1 are less than 7 days!

I need an ISO Week result. I could not find any examples of calendars on the Internet, following the same logic. They all show week 1 from 2014-12-29 to 2015-01-04.

+2
source share
1 answer

Your original post does not mention that you are looking for an ISO week that may have done what you want obscurely.

NET GetWeekOfYear using FirstFourDayWeek and DayOfWeek.Monday almost like an ISO week. The difference is that the ISO week is always seven days. Keep in mind that an ISO date is not only a different format, but also a different calendar with its own terms (for example, high week). On the other hand, your default calandar is Gregorian.

It's easy to configure NET WOY for an ISO week:

 Public Shared Function GetISOWeekOfYear(dt As DateTime) As Integer Dim cal As Calendar = CultureInfo.InvariantCulture.Calendar Dim d As DayOfWeek = cal.GetDayOfWeek(dt) If (d >= DayOfWeek.Monday) AndAlso (d <= DayOfWeek.Wednesday) Then dt = dt.AddDays(3) End If Return cal.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) End Function 

Simple tester for 12/31 / xxxx:

 For n As Integer = 1990 To 2016 Console.WriteLine("{0}: week:{1}", n.ToString, GetISOWeekOfYear(New DateTime(n, 12, 31)).ToString) Next 

A conclusion of only the latter:

2005: week: 52
2006: week: 52
2007: Week: 1 | 2008: week: 1
2009: week: 53
2010: Week: 52
2011: week: 52
2012: Week: 1 | 2013: Week: 1 | 2014: Week: 1
2015: Week: 53
2016: week: 52

+4
source

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


All Articles