14km

Feb 13

Date manipulation

rubyquicktips:

You can add or subtract days or month from a Date object:

  • +(n): add n number of days
  • -(n): subtract n number of days
  • >>(n): add n number of months
  • <<(n): subtract n number of months

Here are some examples:

$ irb
>> date = Date.today     # => #<Date: ...>
>> date.to_s
=> "2010-01-26"
>> tomorrow = date + 1   # => #<Date: ...>
>> tomorrow.to_s
=> "2010-01-27"
>> nextmonth = date >> 1 # => #<Date: ...>
>> nextmonth.to_s
=> "2010-02-26"

Read the documentation for a more precise description.


  1. yuyalush reblogged this from rubyquicktips
  2. reddavis reblogged this from rubyquicktips
  3. 14km reblogged this from rubyquicktips
  4. curtisgwapo reblogged this from rubyquicktips
  5. rubyquicktips posted this