Forum: Ruby on Rails Total Date Difference

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.
Rake N. (Guest)
on 2009-01-07 13:40
Hi,

Anyone can help, on how to get the total number of days with this data;

{:date_created=>"2008-12-19", :date_last_updated=>"2008-12-23"}



The display in "puts" should be the total difference.



Thanks in Advance!
Max W. (Guest)
on 2009-01-07 13:55
Ferdie Ferdie wrote:
> Hi,
>
> Anyone can help, on how to get the total number of days with this data;
>
> {:date_created=>"2008-12-19", :date_last_updated=>"2008-12-23"}
>
>
>
> The display in "puts" should be the total difference.
>
>
>
> Thanks in Advance!

Doing sums with Time objects will give you an int or float, representing
seconds. You can then divide that by 86400 to get the time in days.  If
you get a float back, and just want whole days, then just call .to_i or
'round' on it.

eg

puts "Created about #{((yourhash[:date_last_updated] -
yourhash[:date_created])/86400).round } days ago"

If you want it in days and hours, that's a bit more longwinded but you
could write a method to do it.

It always seemed to me that Ruby is missing a 'duration' class that
represents the difference between two times, with nice methods for
displaying the difference in hours, days, months, years etc.  Is there
one and i missed it?
MaD (Guest)
on 2009-01-07 16:23
(Received via mailing list)
somehow my post did not show up. here it is again:
http://wiki.rubyonrails.org/rails/pages/Duration
MaD (Guest)
on 2009-01-07 16:31
(Received via mailing list)
Rake N. (Guest)
on 2009-01-08 06:09
Thanks for the helps ! it's work perfectly.

Thanks Again for all the replay.
This topic is locked and can not be replied to.