From: Daniel Lucraft Date: 2007-07-04T00:19:22+09:00 Subject: Re: How do I add x months to a date Ben Edwards wrote: > I have a date > > my_date = Date.new( 2007, 1, 1 ) > > I then have a variable holding an integer > > months_to_add = 4 > > How do i add 'months_to_add' to my_date'? > > I have spent ages googeling and looking in o'reilly ruby cookbook but no > luck;( > > Any ideas? > > Ben I would do it like this, if I didn't mind loading some helpful ActiveSupport junk: irb> require 'active_support/core_ext/numeric' => true irb> require 'active_support/core_ext/date' => true irb> my_date = Date.new( 2007, 1, 1 ) => # irb> my_time = my_date.to_time => Mon Jan 01 00:00:00 +0000 2007 irb> months_to_add = 4 => 4 irb> new_time = my_time + months_to_add.months => Tue May 01 01:00:00 +0100 2007 irb> new_date = Date.new(new_time.year, new_time.month, new_time.day) => # best, Dan -- Posted via http://www.ruby-forum.com/.