Week numbers in Ruby

How to get the week number from a date

To get the ISO week number (1-53), use date.strftime('%-V').

date is a Date or DateTime object.

To get the corresponding four-digit year (e.g. 2024), use date.strftime('%G').

How to get the date from a week number

To get a Date object representing the start of the week (Monday), use Date.parse("#{year}W#{week}").

year is a four-digit year, and week is an ISO week number (1-53).

Read more about date.strftime() in the Ruby manual.

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.new(year, 12, 28).strftime('%-V').

year is an integer four-digit year.

This is based on the fact that the last week of the year always includes 28 December.

Read more

Learn more about week numbers and the ISO week numbering scheme in this little primer.