Week of Year

Good morning.

Among other things, I’ve made a calendar. While working on the 2016 edition I thought it would be nice to have the week of the year on it. The Xojo date class provides a weekOfYear property, but it is not the ISO standard week of year. The ISO standard has the week start on Monday. It would be useful to add another property (e.g. isoWeek) to the date class.

-Bob

you can extend the date class with a method and implement the right week count for you
the algorithms are certainly available on the web

Maybe something like this would work:

Global Function WeekOfYearISO(Extends d As Xojo.Core.Date) As Integer
  If d Is Nil Then Return -1
  Dim isoDayOfWeek As Integer = d.DayOfWeek - 1
  If isoDayOfWeek < 1 Then isoDayOfWeek = 7
  
  // https://en.wikipedia.org/wiki/ISO_week_date#Calculation
  Dim week As Integer
  week = (d.DayOfYear - isoDayOfWeek + 10) / 7
  
  Return week
End Function

Thanks @Paul Lefebvre!
I was just about to create that calculation myself when I found yours :wink:

Seems to work just fine.
Now, add it to the date class.

WeekOfYearISO function works different on Windows and MacOS.
Use
d.day = 1
d.month = 1
d.year = 2016

Windows shows week number 53
MacOS show week number 0

see this thread:
https://forum.xojo.com/25771-xojo-core-date-weekofyear-needed