How to get the time difference in minutes in rails for two time fields?

I want to calculate the time difference in minutes between two fields of the date_time.like created_at and updated_at fields. I want to get the result as follows:updated_at - created_at = some minutes.

these times are present in the INDIA time zone. how can i do this on rails?

created = article.created_at
updated = article.updated_at

minutes = updated - created
+4
source share
3 answers

Since created_atthey updated_atare of type DateTime, this should work.

created = article.created_at
updated = article.updated_at
minutes = ((updated - created) * 24 * 60).to_i

Explanation:

DateTimes . e-d (28807336643183/28800000000000). , 24*60 ( 24 , - 60 )

():

d = DateTime.now
 => Tue, 13 Jun 2017 10:21:59 +0530 
2.3.3 :003 > e = DateTime.now + 1.day
 => Wed, 14 Jun 2017 10:22:21 +0530
g = ((e-d) * 24 * 60).to_i
 => 1440
+7

ActiveRecord, TimeWithZone created_at updated_at.

created = article.created_at
updated = article.updated_at

minutes = (updated - created) / 1.minutes
+6

, , , time_ago_in_words Datetime, Date Time, , . time_ago_in_words(@post.created_at)

0

All Articles