# Relative Date Operations

**URL:** <https://www.ruby-forum.com/t/relative-date-operations/66220>\
**Category:** Rails\
**Created:** [August 24, 2006, 2:24am UTC](https://www.ruby-forum.com/t/relative-date-operations/66220 "2006-08-24T02:24:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/43a26b/32.png) [@Marston](https://www.ruby-forum.com/u/Marston)\
**Post date:** [August 24, 2006, 2:24am UTC](https://www.ruby-forum.com/t/relative-date-operations/66220/1 "2006-08-24T02:24:51Z")

</div>

Quick question:

Lets say I have a date as a string such as:

date = ‘2006-08-24’

Whats the easiest way to change that to a date object and so things  
like this:

date.yesterday # which should be 2006-08-24

or

date.tomorrow # should be 2006-08-25

or say

date = ‘2006-08-31’

date.tomorrow # which should be 2006-09-01

Basically I need to be able to find the previous and next day, for any  
date. I know this is probably a simple answer but thanks for any  
help/advice in advance

---

<div class="post-metadata">

**Author:** ![Marston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/43a26b/32.png) [@Marston](https://www.ruby-forum.com/u/Marston)\
**Post date:** [August 24, 2006, 2:42am UTC](https://www.ruby-forum.com/t/relative-date-operations/66220/2 "2006-08-24T02:42:51Z")

</div>

Hi Marston,

Marston wrote:

> Lets say I have a date as a string such as:
> 
> date = ‘2006-08-24’
> 
> Whats the easiest way to change that to a date object

In Ruby, things are how they behave; not how they’re declared. Have you  
tried to treat ‘date’ as a Date object? I think you’ll be pleasantly  
surprised. Check out the to\_date at [http://api.rubyonrails.org/](http://api.rubyonrails.org/), then  
fire  
up script/console and see what you get when you do stuff like new\_date =  
date +1.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![Marston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/43a26b/32.png) [@Marston](https://www.ruby-forum.com/u/Marston)\
**Post date:** [August 24, 2006, 2:48am UTC](https://www.ruby-forum.com/t/relative-date-operations/66220/3 "2006-08-24T02:48:52Z")

</div>

> Basically I need to be able to find the previous and next day, for any  
> date. I know this is probably a simple answer but thanks for any  
> help/advice in advance

.to\_time

./script/console  
Loading development environment.

> > date = ‘2006-08-24’.to\_time  
> > =\> Thu Aug 24 00:00:00 UTC 2006  
> > date.yesterday  
> > =\> Wed Aug 23 00:00:00 UTC 2006  
> > date.tomorrow  
> > =\> Fri Aug 25 00:00:00 UTC 2006  
> > date.years\_since 2  
> > =\> Sun Aug 24 00:00:00 UTC 2008  
> > date = ‘2006-08-31’.to\_time  
> > =\> Thu Aug 31 00:00:00 UTC 2006  
> > date.tomorrow  
> > =\> Fri Sep 01 00:00:00 UTC 2006

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![Marston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/43a26b/32.png) [@Marston](https://www.ruby-forum.com/u/Marston)\
**Post date:** [August 24, 2006, 2:52am UTC](https://www.ruby-forum.com/t/relative-date-operations/66220/4 "2006-08-24T02:52:52Z")

</div>

Thanks a bunch guys, exactly what I needed.
