# How do I add x months to a date

**URL:** <https://www.ruby-forum.com/t/how-do-i-add-x-months-to-a-date/108439>\
**Category:** Ruby\
**Created:** [July 3, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/how-do-i-add-x-months-to-a-date/108439 "2007-07-03T16:14:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [July 3, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/how-do-i-add-x-months-to-a-date/108439/1 "2007-07-03T16:14:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [July 3, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/how-do-i-add-x-months-to-a-date/108439/2 "2007-07-03T17:04:47Z")

</div>

You can’t do it directly, date objects are immutable.

You could always get the date value from the object and then parse it,  
add  
the number of months you want and then create a new date object. That  
seems  
like a bit of a waste though. I would keep the date value in a string  
and  
manipulate it there, if and when I needed an actual date object with  
that  
value I would create a date object and give that out.

On 7/3/07, Ben E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (email address this email is sent from may be defunct)

–  
“Hey brother christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [July 3, 2007, 5:12pm UTC](https://www.ruby-forum.com/t/how-do-i-add-x-months-to-a-date/108439/3 "2007-07-03T17:12:48Z")

</div>

And when I say string you could do better and use a hash or an array.

On 7/3/07, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 7/3/07, Ben E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > (email address this email is sent from may be defunct)

–  
“Hey brother christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [July 3, 2007, 5:19pm UTC](https://www.ruby-forum.com/t/how-do-i-add-x-months-to-a-date/108439/4 "2007-07-03T17:19:17Z")

</div>

Ben E. 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 )  
=\> #\<Date: 4908203/2,0,2299161\>

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)  
=\> #\<Date: 4908443/2,0,2299161\>

best,  
Dan

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [July 3, 2007, 5:23pm UTC](https://www.ruby-forum.com/t/how-do-i-add-x-months-to-a-date/108439/5 "2007-07-03T17:23:49Z")

</div>

On 03/07/07, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > manipulate it there, if and when I needed an actual date object with that  
> > value I would create a date object and give that out.

This all seems like a lot of messing around for what should be a  
standard operation. This type of thing must exist in one of the  
standard libraries.

Ben

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [July 3, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/how-do-i-add-x-months-to-a-date/108439/6 "2007-07-03T17:35:35Z")

</div>

Ben E. wrote:

> On 03/07/07, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > manipulate it there, if and when I needed an actual date object with that  
> > > value I would create a date object and give that out.
> 
> This all seems like a lot of messing around for what should be a  
> standard operation. This type of thing must exist in one of the  
> standard libraries.
> 
> Ben

Actually, you are totally right. The ‘\>\>’ operator does what is  
required:

irb\> my\_date = Date.new( 2007, 1, 1 )  
=\> #\<Date: 4908203/2,0,2299161\>

irb\> my\_date.to\_s  
=\> “2007-01-01”  
irb\> (my\_date \>\> 4).to\_s  
=\> “2007-05-01”

best,  
Dan

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [December 7, 2007, 11:53am UTC](https://www.ruby-forum.com/t/how-do-i-add-x-months-to-a-date/108439/7 "2007-12-07T11:53:57Z")

</div>

Daniel L. wrote:

> Ben E. wrote:
> 
> > On 03/07/07, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > > manipulate it there, if and when I needed an actual date object with that  
> > > > value I would create a date object and give that out.
> > 
> > This all seems like a lot of messing around for what should be a  
> > standard operation. This type of thing must exist in one of the  
> > standard libraries.
> > 
> > Ben
> 
> Actually, you are totally right. The ‘\>\>’ operator does what is  
> required:
> 
> irb\> my\_date = Date.new( 2007, 1, 1 )  
> =\> #\<Date: 4908203/2,0,2299161\>
> 
> irb\> my\_date.to\_s  
> =\> “2007-01-01”  
> irb\> (my\_date \>\> 4).to\_s  
> =\> “2007-05-01”
> 
> best,  
> Dan

I couldn’t find any info about this operator \>\> with dates.

So, for example, if I have 2007-01-01 and I want to know the last day of  
adding three months (2007-03-31) I would do:

d = Date.new(2007,1,1)  
((d \>\> 3)-1).to\_s

it works, but we can relay on this operations ?

regards,

rai

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [December 7, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/how-do-i-add-x-months-to-a-date/108439/8 "2007-12-07T13:33:39Z")

</div>

Ben E. 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 have not tried this but I know of something in rails that sounds  
similar:  
[http://railshelp.com/ActiveSupport::CoreExtensions::Date::Calculations#months\_ago](http://railshelp.com/ActiveSupport::CoreExtensions::Date::Calculations#months_ago)

What if you could do something like this:

my\_date = Date.new( 2007, 1, 1 )  
months\_to\_add = 4  
my\_date = my\_date.months\_ago(months\_to\_add \* -1)

Negative of months\_ago could do it.

Shot in the dark but GL!

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [December 7, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/how-do-i-add-x-months-to-a-date/108439/9 "2007-12-07T14:57:53Z")

</div>

Lloyd L. wrote:

> Ben E. 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 have not tried this but I know of something in rails that sounds  
> similar:  
> [http://railshelp.com/ActiveSupport::CoreExtensions::Date::Calculations#months\_ago](http://railshelp.com/ActiveSupport::CoreExtensions::Date::Calculations#months_ago)

I wasn’t aware of this, thanks !!!

I’m using Rails so I’ll try to use it.

I prefer to do in Ruby if it’s possible …

> What if you could do something like this:
> 
> my\_date = Date.new( 2007, 1, 1 )  
> months\_to\_add = 4  
> my\_date = my\_date.months\_ago(months\_to\_add \* -1)
> 
> Negative of months\_ago could do it.
> 
> Shot in the dark but GL!

I’ll try it …

thanks,

rai

---

<div class="post-metadata">

**Author:** ![benemson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a8b319/32.png) [@benemson](https://www.ruby-forum.com/u/benemson)\
**Post date:** [December 7, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/how-do-i-add-x-months-to-a-date/108439/10 "2007-12-07T17:46:07Z")

</div>

On 12/7/07, Raimon Fs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I couldn’t find any info about this operator \>\> with dates.
> 
> So, for example, if I have 2007-01-01 and I want to know the last day of  
> adding three months (2007-03-31) I would do:
> 
> d = Date.new(2007,1,1)  
> ((d \>\> 3)-1).to\_s
> 
> it works, but we can relay on this operations ?

It’s standard Ruby

## shadowfax:~/ssanta rick$ qri “Date#\<\<” ---------------------------------------------------------------- Date#\<\< \<\<(n)

```
 Return a new Date object that is n months earlier than the current
 one.

 If the day-of-the-month of the current Date is greater than the
 last day of the target month, the day-of-the-month of the returned
 Date will be the last day of the target month.

```

## shadowfax:~/ssanta rick$ qri “Date#\>\>” ---------------------------------------------------------------- Date#\>\> \>\>(n)

```
 Return a new Date object that is n months later than the current
 one.

 If the day-of-the-month of the current Date is greater than the
 last day of the target month, the day-of-the-month of the returned
 Date will be the last day of the target month.

```

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
