16

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
1
  • Could you clear some things here. What do you really meant by date_time fields? The fields of datatype DateTime?
    – Pavan
    Jun 14, 2017 at 4:44

4 Answers 4

40

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
2
  • This will work if created_at/updated_at are instances of Time. Which they aren't, as claimed in Pavan's answer. I'm wondering, can it depend on an application setting or rails version or something? Jun 13, 2017 at 5:44
  • This is clean and more intuitive since it indicates that the resulting units will be minutes. Thanks. Oct 13, 2022 at 14:55
19

As created_at and updated_at are of type DateTime, this should work.

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

Explanation:

Subtracting two DateTimes returns the elapsed time in days. In the below example e-d will return (28807336643183/28800000000000). So to convert this into minutes, we need to multiply it by 24*60(As the day has 24 hours and each hour has 60 minutes)

Example(tested):

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
18
  • 6
    Sorry but how does this work? updated - created will give you a time difference in seconds, which should be divided by 60 to give you minutes. I have no idea why you're multiplying these seconds by the number of minutes in a day...
    – toxaq
    Aug 25, 2016 at 21:44
  • 5
    This answer is incorrect. I've put the correct answer below
    – Doug
    Aug 31, 2016 at 16:02
  • 1
    @pavan I queried it first with no reply. It's incredibly astute of you to noticed that created_at and updated_at have gone against the convention of Rails and used Datetime fields instead of the standard timestamp. Kudos to you! I think that deserves some extra explanation considering a number of people have made that exact query and provided the solution as per convention. Again, well done you for spotting the devil in the detail.
    – toxaq
    Jun 13, 2017 at 9:17
  • 1
    @pavan haha, never not friends! My initial query was an echo in the dark :P Can you add some clarification around timestamp vs datetime for others like Doug and myself that got thoroughly confused? I've deleted my answer already.
    – toxaq
    Jun 13, 2017 at 9:24
  • 1
    @pavan The question doesn't say DateTime, it says date_time which is not a real thing. It also says date time which is also not a thing. Most people coming to this question from Google are going to be referring to ActiveRecord models with TimeWithZone (this is how I discovered it), and your answer is incorrect in that case. I assume that OP meant Article was an ActiveRecord model, and that created_at and updated_at are the default TimeWithZone, not DateTime OP also says times are in Time Zone of India. DateTime does not contain that information.
    – Doug
    Jun 13, 2017 at 20:02
1

looking at helper time_ago_in_words, we can say:

    from_time = from_time.to_time if from_time.respond_to?(:to_time)
    to_time = to_time.to_time if to_time.respond_to?(:to_time)
    #absolute distance:
    from_time, to_time = to_time, from_time if from_time > to_time
    distance_in_minutes = ((to_time - from_time)/60.0).round
-1

This is what i used and it simply takes care of days or Min's for you as well as giving you the difference time_ago_in_words simply pass a Datetime, Date or Time object and it will return the difference that is human readable. time_ago_in_words(@post.created_at)

1
  • 5
    Dwonvoted. The question asks for the result in minutes not as a string sentence. Jun 12, 2017 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.