This is an example of a week, from Sunday to Saturday:
11/21/2010 - 11/27/2010
I would like to find the dates for the same week Sunday-Saturday, only for last year.
require 'date' # Included in Ruby's standard library, no gem needed
now = Date.today
before = Date.civil( now.year-1, now.month, now.day )
sunday = Date.commercial( before.year, before.cweek, 1 ) - 1 # Day 1 is Monday
this_week_last_year = sunday..(sunday+6)
Edit: Though Date.commercial
is cool, it's not needed. Here's a Simpler way to find the Sunday starting the week:
require 'date'
now = Date.today
before = Date.civil( now.year-1, now.month, now.day )
sunday = before - before.wday
>> 1.year.ago.beginning_of_week.to_date
Mon, 30 Nov 2009
>> 1.year.ago.end_of_week.to_date
Sun, 06 Dec 2009
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With