# Date additon / subtraction - just the basics

**URL:** <https://www.ruby-forum.com/t/date-additon-subtraction-just-the-basics/84904>\
**Category:** Rails\
**Created:** [February 8, 2007, 3:59pm UTC](https://www.ruby-forum.com/t/date-additon-subtraction-just-the-basics/84904 "2007-02-08T15:59:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![canner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@canner](https://www.ruby-forum.com/u/canner)\
**Post date:** [February 8, 2007, 3:59pm UTC](https://www.ruby-forum.com/t/date-additon-subtraction-just-the-basics/84904/1 "2007-02-08T15:59:11Z")

</div>

How do you manipulate dates?

I take a string x = “2/4/2007”.to\_date

then I want to add 5 days to it returning “2/9/2007” as a date

I could just pull it apart and add the days to it, but if it should  
switch the month then it becomes a problem.

I saw things like 1.week.ago but that deals with the current date, and  
x.1.week.ago returns an error.

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![canner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@canner](https://www.ruby-forum.com/u/canner)\
**Post date:** [February 8, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/date-additon-subtraction-just-the-basics/84904/2 "2007-02-08T16:15:29Z")

</div>

Try making it a Time object instead of a Date:

> > t = Time.mktime(2007,2,4)  
> > =\> Sun Feb 04 00:00:00 CST 2007
> 
> > t.ago(2.days)  
> > =\> Fri Feb 02 00:00:00 CST 2007
> 
> > 5.days.since(t)  
> > =\> Fri Feb 09 00:00:00 CST 2007

You can call to\_date on the Time object if you actually need a Date  
object  
finally.

ed

---

<div class="post-metadata">

**Author:** ![canner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@canner](https://www.ruby-forum.com/u/canner)\
**Post date:** [February 8, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/date-additon-subtraction-just-the-basics/84904/3 "2007-02-08T16:16:39Z")

</div>

Alex T. wrote:

> How do you manipulate dates?
> 
> I take a string x = “2/4/2007”.to\_date
> 
> then I want to add 5 days to it returning “2/9/2007” as a date
> 
> I could just pull it apart and add the days to it, but if it should  
> switch the month then it becomes a problem.
> 
> I saw things like 1.week.ago but that deals with the current date, and  
> x.1.week.ago returns an error.
> 
> Thanks for any help.

x = x.advance(:days =\> 5)

---

<div class="post-metadata">

**Author:** ![canner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@canner](https://www.ruby-forum.com/u/canner)\
**Post date:** [February 8, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/date-additon-subtraction-just-the-basics/84904/4 "2007-02-08T16:19:46Z")

</div>

James B. wrote:  
should be:

x = x.to\_time.advance(:days =\> 5)

---

<div class="post-metadata">

**Author:** ![canner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@canner](https://www.ruby-forum.com/u/canner)\
**Post date:** [February 8, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/date-additon-subtraction-just-the-basics/84904/5 "2007-02-08T16:27:30Z")

</div>

check this :

> **[Select a Reject — err.the\_blog](http://errtheblog.com/post/44)**
>
> Err the Blog: Ruby and Rails from the front.

x = “2/4/2007”.to\_time  
y = 1.week.ago(x)  
z = 2.days.since(x)

Charly

---

<div class="post-metadata">

**Author:** ![canner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@canner](https://www.ruby-forum.com/u/canner)\
**Post date:** [February 8, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/date-additon-subtraction-just-the-basics/84904/6 "2007-02-08T16:20:47Z")

</div>

James B. wrote:

> James B. wrote:  
> should be:
> 
> x = x.to\_time.advance(:days =\> 5)

and to return a date object

x = x.to\_time.advance(:days =\> 5).to\_date

---

<div class="post-metadata">

**Author:** ![canner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@canner](https://www.ruby-forum.com/u/canner)\
**Post date:** [February 8, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/date-additon-subtraction-just-the-basics/84904/7 "2007-02-08T17:14:23Z")

</div>

You guys rock! Thanks for the help!
