Exact Difference between two dates in days and hours

ruby, ruby-on-rails, time

Solution

There's a gem for that: `time_diff` !

https://rubygems.org/gems/time_diff

It will display the time difference and you can even format the output. Example:

`'%y, %M, %w, %d and %h:%m:%s'` will return: `'1 year, 2 months, 3 weeks, 4 days and 12:05:52'`

The syntax is also very straighforward:

> Time.diff(Time.parse('2010-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), '%y, %d and %h:%m:%s')
=> {:year => 1, :month => 0, :week => 0, :day => 0, :hour => 18, :minute => 0, :second => 30, :diff => '1 year and 18:00:30'}

You should be able to get what you need out of this.

Happy coding !

Problem

I want to get exact time in days and hours between two dates something like between `Thu Jul 18 11:30:00 +0000 2013` and `Sat Jul 20 10:30:00 +0000 2013`. There is difference of `1 day and 23 hours`. The Rails' ActionView module includes two methods ``` (1) distance_of_time_in_words (2) distance_of_time_in_words_to_now ``` but they return result like 1 day or 2 day i need hours also.

Original source