Finding the date for a given week number

You want Date.commercial:

require 'date'
now = Date.today                                           #=> 2012-03-05
monday_next_week = Date.commercial(now.cwyear,now.cweek+1) #=> 2012-03-12
next_sunday_or_today = monday_next_week - 1                #=> 2012-03-11

Note that weeks start on Monday, so if you are on a Sunday and ask for next monday - 1 you'll get the same day.

Note also that if you don't want Mondays you can also specify the day number in the method:

thursday_next_week = Date.commercial(now.cwyear,now.cweek+1,4) #=> 2012-03-15

Calculating on a day basis is pretty simple with Date objects. If you just want to get the previous / next week from a given Date object use the following:

date = Date.today
previous_week = (date - 7).cweek
next_week = (date + 7).cweek

Tags:

Ruby

Date