next_week
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3 (0)
- 2.1.0 (0)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
next_week(day = :monday)
public
Returns a new Date/DateTime representing the start of the given day in next week (default is Monday).
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.
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'