Is there an add_days in ruby datetime?

The Date class provides a + operator that does just that.

>> d = Date.today
=> #<Date: 4910149/2,0,2299161>
>> d.to_s
=> "2009-08-31"
>> (d+3).to_s
=> "2009-09-03"
>> 

In Rails there are very useful methods of Fixnum class for this (here n is Fixnum. For example: 1,2,3.... ):

Date.today + n.seconds # you can use 1.second
Date.today + n.minutes # you can use 1.minute
Date.today + n.hours # you can use 1.hour
Date.today + n.days # you can use 1.day
Date.today + n.weeks # you can use 1.week
Date.today + n.months # you can use 1.month
Date.today + n.years # you can use 1.year

These are convenient for Time class too.

PS: require Active Support Core Extensions to use these in Ruby

require 'active_support/core_ext'

Tags:

Ruby