Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a DateTime duration?

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.

like image 834
Jesse Avatar asked Dec 29 '08 17:12

Jesse


2 Answers

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.

like image 103
Sören Kuklau Avatar answered Sep 18 '22 16:09

Sören Kuklau


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

like image 35
Ryan Bigg Avatar answered Sep 20 '22 16:09

Ryan Bigg