Andrew
Andrew

Reputation: 238667

Finding the date for a given week number

I am trying to do some date math based on the week number of a given year. For example:

date = Date.today # Monday, March 5, 2012
puts date.cwyear  # 2012
puts date.cweek   # 10 (10th week of 2012)

Now that I know what the current week is, I want to figure out what the next week and previous week are. I need to take the year (2012) and the week number (10) and turn it back into a date object so I can calculate the value for the next/previous week. How can I do this?

Upvotes: 7

Views: 4059

Answers (5)

Arvind singh
Arvind singh

Reputation: 1392

Assuming you mean "a given week number in the current year", you can do the following:

2.weeks.since(Time.gm(Time.now.year))
=> Fri Jan 15 00:00:00 UTC 2010

Substitute (week_number - 1) for the 1 in the above, and you'll get a date in the desired week.

Upvotes: 0

Thorin
Thorin

Reputation: 2034

I have created some methods to get week number of a given date something like this:

def self.get_week(date)
    year = date.year
    first_monday_of_the_year = self.get_first_monday_of_the_year(year)
    # The first days of January may belong to the previous year!
    if date < first_monday_of_the_year
      year -= 1
      first_monday_of_the_year = self.get_first_monday_of_the_year(year)
    end
    day_difference = date - first_monday_of_the_year
    week = (day_difference / 7).to_i + 1
    week
  end

   def self.get_monday_of_year_week(year, week)
    d = self.get_first_monday_of_the_year(year)
    d + ((week - 1) * 7).days
  end

  def self.get_first_monday_of_the_year(year)
    d = Date.new(year, 1, 7)  # 7 Jan is always in the first week
    self.get_monday_of_week(d)
  end

  def self.get_monday_of_week(date)
    wday = (date.wday + 6) % 7
    date - wday.days
  end

Upvotes: 0

Phrogz
Phrogz

Reputation: 303178

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

Upvotes: 15

Hauleth
Hauleth

Reputation: 23556

In ActiveSupport you have helper to convert Fixnum to time http://as.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Numeric/Time.html use:

date = Date.today
week_ago = date - 1.week
next_week = date + 1.week

Upvotes: 1

Koraktor
Koraktor

Reputation: 42893

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

Upvotes: 3

Related Questions