Time difference in hours

Viewed 63669

I am trying to get the difference in hours for two different Time instances. I get these values from the DB as a :datetime column

How can I do this so that it includes the months and years as well in the calculation while ignoring or rounding the minutes? Can this only be done manually or is there a function to do this?

7 Answers
((date_2 - date_1) / 3600).round

or

((date_2 - date_1) / 1.hour).round

In Rails, there is now a far more natural way to achieve this:

> past = Time.now.beginning_of_year
2018-01-01 00:00:00 +0100

> (Time.now - past) / 1.day
219.50031326506948
Related