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 terms of hours between the two time objects?
hours = created - updated
I'd like to wrap this in a method and extend the Time class. I find it hard to believe I'd need to extend it, but I can't seem to find a native method that handles calculating elapsed time using different time units.
This should work:
Related question: Rails Time difference in hours
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.
Another option would be to use distance_of_time_in_words helper:
I hope you find it useful :)