Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Jan 26 ’10

Date manipulation

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.

51 notes 0 comments

  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

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus