views:

537

answers:

4

Hello, I'm baffled how to do this.

I need to take a datetime object and get the duration in hours, days, whatever, to the current time.

Thank you.

+2  A: 

You want the Time class rather than DateTime.

Charlie Martin
+5  A: 

Getting the duration in seconds is easy:

>> foo = Time.new
=> Mon Dec 29 18:23:51 +0100 2008
>> bar = Time.new
=> Mon Dec 29 18:23:56 +0100 2008
>> print bar - foo
5.104063=> nil

So, a little over five seconds.

But to present this in a somewhat more human-friendly form, you'll need a third-party addition, like time_period_to_s, or the Duration package.

Sören Kuklau
+1  A: 

If you are using rails, then distance_of_time_in_words_to_now may be what you are looking for.

anithri
+1  A: 

distance_of_time_in_words will give you a string representation of the time between two time/date objects.

Ryan Bigg