Date Functions


#1

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


#2

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