method

next_week

Importance_2
Ruby on Rails latest stable (v7.1.3.2) - 2 notes - Class: ActiveSupport::CoreExtensions::Date::Calculations

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v2.3.8) is shown here.

These similar methods exist in v7.1.3.2:

next_week(day = :monday) public

Returns a new Date/DateTime representing the start of the given day in next week (default is Monday).

Show source
Register or log in to add new notes.
February 16, 2009
1 thank

Reverse of this

If you want to do the reverse of this, e.g. go from a specific date and back to a certain day of the previous week, you can implement it like this:

def last_week(day = :monday)
  days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
  result = (self - 7).beginning_of_week + days_into_week[day]
  self.acts_like?(:time) ? result.change(:hour => 0) : result
end

If you do not want to make your own method of this, but just want to do it in a regular chaining of date methods (like Date.today.next_year.at_midnight), you can do it like the following:

(date - 7).next_week(:tuesday) # Tuesday, last week

Please note that you just need to subtract 7 if you want to move back a week. Only use these methods if you want to go to a specific day of the week.

September 6, 2009
0 thanks

Add last_week to core_extensions

If you want to implement last_week as posted by Mange, save it as:

lib/core_extensions.rb

class Date
  def last_week(day = :monday)
    days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
    result = (self - 7).beginning_of_week + days_into_week[day]
    self.acts_like?(:time) ? result.change(:hour => 0) : result
  end
end

And add to:

config/environment.rb

require 'core_extensions'