Let\'s say I create two time objects from my user model:
created = u.created_at
updated = u.updated_at
How do I calculate the difference in
Another option would be to use distance_of_time_in_words helper:
<%= distance_of_time_in_words u.created_at, u.updated_at %>
I hope you find it useful :)
I would like to add an alternate answer using a rails-specific method. The Time class has a method called minus_with_coercion. It compares two times and returns a result in seconds.
hours=(created.minus_with_coercion(updated)/3600).round
This should work:
hours = ((created - updated) / 1.hour).round
Related question: Rails Time difference in hours