Forum: Ruby on Rails distance_of_time_in_words and Time classes

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.
Kris L. (Guest)
on 2006-03-29 19:59
There is <%= distance_of_time_in_words(Time.now, @a_time) %> before
xzy.<BR />
<!-- ^ this gives 2279 days -->

This does not work because when @a_time is stored, even though it is a
Time field and the class is Time, it includes a date. The Date isn't
even that days date its the year 2000, hence the 2279 days ago.

So how do I set the date part of @a_time (a Time class) to today's date
so that distance_of_time_in_words works as expected?


Cheers K.

AFM
kris (Guest)
on 2006-04-05 18:00
Any ideas anyone?  :)

Kris L. wrote:
> There is <%= distance_of_time_in_words(Time.now, @a_time) %> before
> xzy.<BR />
> <!-- ^ this gives 2279 days -->
>
> This does not work because when @a_time is stored, even though it is a
> Time field and the class is Time, it includes a date. The Date isn't
> even that days date its the year 2000, hence the 2279 days ago.
>
> So how do I set the date part of @a_time (a Time class) to today's date
> so that distance_of_time_in_words works as expected?
>
>
> Cheers K.
>
> AFM
Alan F. (Guest)
on 2006-04-05 18:35
kris wrote:
> Any ideas anyone?  :)

Is Time immutable ?

You may have to remove multiple days worth of seconds to get the sub-day
part.

difference_in_time_in_words( (Time.now - @a_time) mod (60 * 60 * 12) )

Alan
This topic is locked and can not be replied to.