Week numbers in Ruby
How to get the week number from a date
To get the ISO week number (1-53), use date.cweek.
date is a Date or DateTime object.
To get the corresponding four-digit year (e.g. 2026), use date.cwyear.
Read more about Date#cweek and Date#cwyear in the Ruby docs.
How to get the date from a week number
To get a Date object representing the start of the week (Monday), use Date.commercial(year, week).
year is a four-digit year, and week is an ISO week number (1-53).
Read more about Date.commercial in the Ruby docs.
How to get the number of weeks in a year
To get the number of ISO weeks (i.e. the number of the last week) in a year, use Date.valid_commercial?(year, 53, 1) ? 53 : 52.
year is an integer four-digit year.
There are always either 52 or 53 weeks in a year, so you just check whether week 53 exists.
Read more about Date.valid_commercial? in the Ruby docs.
Read more
Learn more about week numbers and the ISO week numbering scheme in this little primer.