Rails 4: How to add/subtract multiples of quarters to Date?

I would do it this way:

def quarter_dates(offset)
  date = Date.today << (offset * 3)
  [date.beginning_of_quarter, date.end_of_quarter]
end

puts quarter_dates(0) #=> [Tue, 01 Apr 2014, Mon, 30 Jun 2014]
puts quarter_dates(1) #=> [Wed, 01 Jan 2014, Mon, 31 Mar 2014]
puts quarter_dates(9) #=> [Sun, 01 Jan 2012, Sat, 31 Mar 2012]

Using Rails my solution is

def quarter_dates(quarter_offset = 0)
  quarter_start = Time.now.to_date.beginning_of_quarter - (3 * quarter_offset).months
  quarter_end = quarter_start.end_of_quarter
  [quarter_start,quarter_end]
end

I personally would prefer to use + (3*quarter_offset).months and use negative offsets for previous months though.


I assume positive offsets for future and negative offsets for past. You can make negative offsets for future and positive offsets for the past by replacing months_since with months_ago.

You can also optionally pass a day if you wish to get quarters relative to it.

def quarter_dates offset=0, date_day=Date.today
  temp = date_day.months_since(3 * offset)
  [temp.beginning_of_quarter, temp.end_of_quarter]
end

Test:

quarter_dates # => [Tue, 01 Apr 2014, Mon, 30 Jun 2014] 
quarter_dates -1 # => [Wed, 01 Jan 2014, Mon, 31 Mar 2014]
quarter_dates -2, Date.new(2013,1,1) # => [Sun, 01 Jul 2012, Sun, 30 Sep 2012] 
quarter_dates 1, Date.new(2013,9,30) # => [Tue, 01 Oct 2013, Tue, 31 Dec 2013]