假设记录有一个created_at时间戳为Fri, 16 Feb 2018 18:40:53 UTC +00:00,我想找出从当前时间过去的天数。我怎样才能做到这一点?
以下是我尝试过的:
(Fri, 16 Feb 2018 18:40:53 UTC +00:00 - Time.now).to_i但这行不通。
发布于 2018-06-13 02:27:07
差不多,但是Ruby不理解你写约会时间的方式。
require 'date'
(DateTime.now - DateTime.new(2018, 2, 16, 18, 40, 53, 0)).to_i发布于 2018-06-13 05:14:38
In rails如果从数据库获得模型User的时间戳(而不是要解析的时间戳字符串):
seconds_from_creation = Date.today.to_time - user.created_at.to_time或直接转换为天数(to_i循环为整数,检查是否适合您或自定义):
((Date.today.to_time - user.created_at.to_time)/(24*60*60)).to_i在视图中,可以使用以下内容,它返回一个字符串:
time_ago_in_words user.created_at
# => 10 days (in my case)发布于 2018-06-13 02:29:49
您可以使用
require 'time'
created_at = 'Fri, 16 Feb 2018 18:40:53 UTC +00:00'
(Time.now - Time.parse(created_at)).to_i / 86400https://stackoverflow.com/questions/50828181
复制相似问题