计算两个 Time 对象之间的时间差 [英] Calculating difference in time between two Time objects
本文介绍了计算两个 Time 对象之间的时间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我从我的用户模型中创建了两个时间对象:
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 terms of hours between the two time objects?
hours = created - updated
我想将其包装在一个方法中并扩展 Time 类.我发现很难相信我需要扩展它,但我似乎无法找到一种本地方法来处理使用不同时间单位计算经过的时间.
I'd like to wrap this in a method and extend the Time class. I find it hard to believe I'd need to extend it, but I can't seem to find a native method that handles calculating elapsed time using different time units.
推荐答案
这应该有效:
hours = ((created - updated) / 1.hour).round
相关问题:Rails 时差(以小时为单位)
这篇关于计算两个 Time 对象之间的时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文