2017-02-18 100 views
0

我在我的应用程序中有一个Timekeeper模型。它跟踪起始时间和完成时间。我想取这两个datetimes之间的差异,并提取秒并将该值作为整数存储在数据库中。这是我目前的工作。在数据库中存储秒 - Rails

方法:

((timekeeper.started_at - Time.current) * 24 * 60 * 60).to_i 

这将返回一个类似如下的值:-2336036

上述两个时间只差12秒分开,我得到的价值。我不确定它在说什么。

从我的问题的外卖是:How can I store seconds in the database, between two datetimes that looks similar to what I'm doing currently?

回答

1

如果你想找到的2倍,你只需要做减法的差异,以秒返回的区别。

time.started_at - Time.current 

,如果你不想的第二

(time.started_at - Time.current).to_i 

((time.started_at - Time.current).to_i).abs 

尝试在轨控制台上面的比例,且绝对值