how to get time difference in minutes in rails for two date time fields?

John picture John · Jun 20, 2015 · Viewed 15.6k times · Source

i want to calculate time difference in minutes between two date_time fields.like created_at and updated_at kind of fields. i want the result like this updated_at - created_at = some minutes.

these times are present in time zone of INDIA. how can i do that in rails?

created = article.created_at
updated = article.updated_at

minutes = updated - created

Answer

Doug picture Doug · Aug 31, 2016

This solution works for ActiveRecord models using TimeWithZone classes for created_at and updated_at.

created = article.created_at
updated = article.updated_at

minutes = (updated - created) / 1.minutes