Total Date Difference


#1

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!


#2

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?


#3

somehow my post did not show up. here it is again:
http://wiki.rubyonrails.org/rails/pages/Duration


#4

you could use this one:
http://wiki.rubyonrails.org/rails/pages/Duration


#5

Thanks for the helps ! it’s work perfectly.

Thanks Again for all the replay.