Determine date range given year and week number

How can I get the date range (or just the Date of the Monday) of that week?
For example, I have year = 2023 week=5, somehow I need to know that the Monday of that week is 2023-01-30.
Using a relevant contructor from Dates · The Julia Language does not help, it just does not work sadly:

julia> Date(Dates.Week(5), Dates.Year(2023),)
2023-01-01

Issue in julia posted `Date(Dates.Week(5), Dates.Year(2023))` (week, year) constructor is not working · Issue #48490 · JuliaLang/julia · GitHub

1 Like

Is this what you need?

julia> using Dates

julia> year = 2023
2023

julia> week = 5
5

julia> monday = firstdayofweek(Date(Year(year)) + Week(week))
2023-01-30
4 Likes

And for the range from Monday to Friday:

using Dates
y = 2023
w = 5
monday = firstdayofweek(Week(w) + Date(Year(y)))
r = monday:(monday+Day(4))      # range Mon to Fri
1 Like

Thanks all, I’m not so sure about the correctness of these solutions. Because for some reason definition of the first week of the year is a little strange. Excerpt from wiki

An ISO week-numbering year (also called ISO year informally) has 52 or 53 full weeks. That is 364 or 371 days instead of the usual 365 or 366 days. These 53 week years occur on all years that have Thursday as the 1st of January and on leap years that start on Wednesday the 1st. The extra week is sometimes referred to as a leap week, although ISO 8601 does not use this term.

I’m almost sure that Julia adheres to this. I’m afraid the solutions you posted might not be always correct. I know better than implementing my own datetime logic, as there could be so many edge cases.

Your solution looks nice, but somehow I need to account for this weird definition of week number. And if we account for this maybe it worth including this in julia std lib

Is it helpful? (if so, it deserves some testing)

function firstmondayofISOyear(year)
  date = Date(Year(year))
  firstthursdayofyear = firstdayofweek(date) + Day(3)
  yr = Year(firstthursdayofyear).value
  if yr < year
    firstthursdayofyear += Day(7)
  end
  firstthursdayofyear - Day(3)
end

also

help?> Dates.week
  week(dt::TimeType) -> Int64


  Return the ISO week date
  (https://en.wikipedia.org/wiki/ISO_week_date) of a Date or DateTime
  as an Int64. Note that the first week of a year is the week that
  contains the first Thursday of the year, which can result in dates
  prior to January 4th being in the last week of the previous year. For
  example, week(Date(2005, 1, 1)) is the 53rd week of 2004.

  Examples
  ≡≡≡≡≡≡≡≡

  julia> week(Date(1989, 6, 22))
  25

  julia> week(Date(2005, 1, 1))
  53

  julia> week(Date(2004, 12, 31))
  53
1 Like

This looks great, thank you. Perhaps this could be tested and extended such that,
Date(Dates.Week(5), Dates.Year(2023),) would work in std lib of julia. I’ll try to get to do that if you allow me to use your code

Yes I think the following functions should be defined for any TimeType:

  • isoyear (like week but giving the year)
  • firstdayofisoyear (like firstdayofyear but for the ISO year)
  • lastdayofisoyear
  • weeksinyear (similar to daysinmonth)

And extend the Date and DateTime constructors to accept a Week period too. It should then be documented that a Week or Month can be in the parameters, but not both. And if a Week is given, then Year is interpreted as ISO year and Day as day in the week.

You have my permission to do whatever you like with this.