Reputation: 475
In my app I want to print out the duration of time I spent from I made the model until I updated it.
So if I have the value
:created_at set to 2016-04-13 14:00:49 UTC and :updated_at set to 2016-04-13 15:05:49 UTC
I want to print out that it took 1hour and 5minutes. (or just 01.05). How do I do this?
Upvotes: 1
Views: 1419
Reputation: 1012
You can try this
diff [email protected]_at.to_time.to_i - @object.created_at.to_time.to_i
hour = "#{diff / 3600}:#{(diff % 3600) / 60}"
Upvotes: 0
Reputation: 7339
I'm not sure why this was downvoted, though a little googling would probably have gotten you to the right answer fairly quickly.
What you're looking for is called time_ago_in_words
Here is the doc http://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words
And usage is:
time_ago_in_words @object.created_at
time_ago_in_words @object.updated_at
If you want to use it the console to play with it, make sure you preface it with helper
so it's loaded into console
e.g.
helper.time_ago_in_words @object.created_at
For checking between 2 dates, not just one date from right now, then you can use distance_of_time_in_words
distance_of_time_in_words(@object.created_at, @object.updated_at)
That gives words like
12 days ago
If you're ONLY looking for hours, and nothing else then you can use basic subtraction and division
@object.updated_at.to_i - @object.created_at.to_i) / 60 / 60
Upvotes: 1