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.
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.
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.
If you are using rails, then distance_of_time_in_words_to_now may be what you are looking for.
distance_of_time_in_words
will give you a string representation of the time between two time/date objects.