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:
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
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
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.