Rails时间戳,看看自上次以来经过了多少时间

时间:2016-04-13 14:00:17

标签: ruby-on-rails datetime

在我的应用程序中,我想打印出我制作模型所花费的时间,直到我更新它。

所以,如果我有值

:created_at设置为2016-04-13 14:00:49 UTC 和 :updated_at设置为2016-04-13 15:05:49 UTC

我想打印出来花了1小时5分钟。 (或只是01.05)。 我该怎么做呢?

2 个答案:

答案 0 :(得分:1)

我不确定为什么这会被贬低,虽然有点谷歌搜索可能会让你很快得到正确答案。

您正在寻找的内容称为time_ago_in_words

以下是文档http://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words

用法是:

time_ago_in_words @object.created_at
time_ago_in_words @object.updated_at

如果您想使用控制台来播放它,请确保在其前面添加helper,以便将其加载到控制台

e.g。

helper.time_ago_in_words @object.created_at

更新

要检查两个日期,而不仅仅是现在的一个日期,那么您可以使用distance_of_time_in_words

distance_of_time_in_words(@object.created_at, @object.updated_at)

这给出了像

这样的词
  

12天前

如果你只是在寻找几个小时,那么你可以使用基本的减法和除法

@object.updated_at.to_i - @object.created_at.to_i) / 60 / 60

答案 1 :(得分:0)

你可以试试这个

AdjacencyGraph<T, Edge<T>> newGraphInstance = new AdjacencyGraph<int, Edge<int>>();
newGraphInstance.AddVerticesAndEdge(new Edge<T>(vertex1, vertex2));
相关问题