DateTime: No week 1 in 2021?

Here is a project file for the calendar week problem.
Features:

  • Select a region
  • Set week start on Sunday or Monday
  • Get the calendar week for the selected day

Download project file (zip)

1 Like

Hi, thank you for your project. I allowed myself to add my routine - according to ISO it uses Monday as 1st day of week, anyway.

There you can select your routine as “Horst” and my - as “Pawel”. Please, kindly recognize how it shows days 1 or 2 of January 2020. It is 53rd week vs 1st.

If 1st day of January is Thursday or earlier - then the whole week is 1st week, not the 53rd or 52nd.

WeekOfYear2.xojo_binary_project.zip

1 Like

For some reason I don’t get week 53 as “Horst” or as “Pawel”.

Looks like this code gives the correct ISO Week of Year:

Public Function ISOWeekNumber(extends dt as DateTime) As Integer
  Var myLocale As New Locale("es-ES") //you can use any other
  Return dt.ToString("w",myLocale).ToInteger
End Function
Var sqldate As String = "2021-01-02"
Var mylocale As New Locale("es-ES")
Var dt As DateTime = DateTime.FromString(sqldate) 
// get week of year
Var weekYearUSA As Integer = dt.WeekOfYear //1 on my computer, don't know if varies
Var ISOweek As Integer =  dt.ISOWeekNumber //53, correct week number for the date as first week is the week that has January 4th

my original method was for Date, not DateTime. Try this:

Public Function ISOWeekNumber(extends dt as DateTime) As integer
  //nr tygodnia wg ISO 8601, gdzie poniedziaƂek jest pierwszym dniem tygodnia
  
  var dow,woy as UInt8
  
  dow=dt.DayOfWeek-1
  if dow=0 then dow=7
  woy=Floor((10+dt.DayOfYear-dow)/7)
  
  if woy=0 then
    var dp as new date (dt.Year-1,12,31)
    dow=dp.DayOfWeek-1
    if dow=0 then dow=7
    woy=Floor((10+dp.DayOfYear-dow)/7)
  elseif woy=53 then
    var dp as new date (dt.Year+1,1,1)
    dow=dp.DayOfWeek
    if dow<6 and dow>1 then woy=1
  end if
  
  Return woy
  
End Function

this gives 53rd week according to ISO 8601:

Var sqldate As String = "2021-01-02"
Var mylocale As New Locale("es-ES")
Var dt As DateTime = DateTime.FromString(sqldate) 

MessageBox str(dt.ISOWeekNumber)

Xojo has updated the “w” to show correct week now, apparently.

This functionality comes from the ICU libs. Lots of locale things change every year around the globe, so ICU needs to be updated or current standards may differ.
That’s why apps moving ICU dependency to the OS and the OS being updated are important. If the OS does not offer direct access to an updated ICU from its system, then Xojo should assume ICU from its resource libs.
One very perceptible effect is bad date/time calculations due to Daylight Saving Time changes in many places over the years. Or how week of the year is counted in some region/culture.

I remember trying your method and it worked, even I recommended that on one of the Issues.

But I never tested your WeekOfYear2 code and was surprised that it didn’t show week 53 for Jan 2, 2021.

I got the idea to use ToString Format from another post I did a few days ago, did a few tests and it looks like format “w” works with the right locale to give the correct week of year. Of course this is with DateTime. I guess this worked since Xojo added DateTime ToString format using ICU format.