Your original post does not mention you are looking for ISO week, which may have made what you want unclear.
The NET GetWeekOfYear
using FirstFourDayWeek
and DayOfWeek.Monday
is almost like an ISO Week. The difference is that an ISO Week is always seven days. Keep in mind that an ISO Date is not just a different format, but a different calendar complete with its own terms (like leap week). Your default NET calandar on the other hand is Gregorian.
It is not hard to tweak a NET WOY to 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
Output of just the last few:
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