Forum: Ruby on Rails Date Functions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6f4989f18fb2315d4b1db15cccb29ecf?d=identicon&s=25 Mick (Guest)
on 2007-03-26 18:08
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
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-03-26 18:31
(Received via mailing list)
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
This topic is locked and can not be replied to.