# Date Functions

**URL:** <https://www.ruby-forum.com/t/date-functions/90182>\
**Category:** Rails\
**Created:** [March 26, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/date-functions/90182 "2007-03-26T18:08:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [March 26, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/date-functions/90182/1 "2007-03-26T18:08:24Z")

</div>

Hi,

I would like to be able to do a date comparison to return the number of  
days left until a date (stored in my database) is reached. I can’t seem  
to find a ruby function that will do such an operation and regardless of  
this, other ruby functions throw errors when I try to apply them to this  
date, e.g.

the\_event.start\_on.next\_week()

returns this error:

undefined method `next\_week’ for “2007-03-28 18:30:00”:String

Any suggestions?

Thanks, Mick

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [March 26, 2007, 6:31pm UTC](https://www.ruby-forum.com/t/date-functions/90182/2 "2007-03-26T18:31:47Z")

</div>

Hi Mick,

Mick wrote:

> I would like to be able to do a date comparison to  
> return the number of days left until a date (stored in  
> my database) is reached. I can’t seem to find a ruby  
> function that will do such an operation

In this case, you’re looking for Rails helper modules, not Ruby methods.  
If  
you haven’t already, check out  
ActiveSupport::CoreExtensions::Time::Calculations for more of them.

> and regardless of this, other ruby functions throw  
> errors when I try to apply them to this date, e.g.
> 
> the\_event.start\_on.next\_week()
> 
> returns this error:
> 
> undefined method `next\_week’ for “2007-03-28 18:30:00”:String

next\_week works on a Time object and the\_event.start\_on is apparently a  
String object. If you’re intentionally storing it as a String, you  
could  
use the to\_time conversion before applying the next\_week method.

C:\InstantRails-1.5\rails\_apps\sandbox\>ruby script\console  
Loading development environment.

> > event=Time.now  
> > =\> Mon Mar 26 11:26:25 -0500 2007
> 
> > event\_string = event.to\_s  
> > =\> “Mon Mar 26 11:26:25 -0500 2007”
> 
> > event\_string.to\_time.next\_week  
> > =\> Mon Apr 02 00:00:00 UTC 2007
> 
> >

hth,  
Bill
