Time difference in hours
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?
((date_2 - date_1) / 3600).round
or
((date_2 - date_1) / 1.hour).round
Try Time Difference gem for Ruby at https://rubygems.org/gems/time_difference
start_time = Time.new(2013,1)
end_time = Time.new(2014,1)
TimeDifference.between(start_time, end_time).in_years
this works great, example for number of hours since user created account
(Time.parse(DateTime.now.to_s) - Time.parse(current_user.created_at.to_s))/3600
You can use a gem called time_diff to get the time difference in very useful formats