Calculating difference in time between two Time objects

前端 未结 3 2013
遥遥无期
遥遥无期 2020-12-15 07:20

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

相关标签:
3条回答
  • 2020-12-15 08:00

    Another option would be to use distance_of_time_in_words helper:

    <%= distance_of_time_in_words u.created_at, u.updated_at %>
    

    I hope you find it useful :)

    0 讨论(0)
  • 2020-12-15 08:06

    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.

    hours=(created.minus_with_coercion(updated)/3600).round
    
    0 讨论(0)
  • 2020-12-15 08:12

    This should work:

    hours = ((created - updated) / 1.hour).round
    

    Related question: Rails Time difference in hours

    0 讨论(0)
提交回复
热议问题