我需要知道如何在 Rails 中做相对时间,而不是作为一个句子,更像是我可以做的事情(当我输入像这样的格式 2008-08-05 23:48:04 -0400 时)
if time_ago < 1 hour, 3 weeks, 1 day, etc.
execute me
end
我需要知道如何在 Rails 中做相对时间,而不是作为一个句子,更像是我可以做的事情(当我输入像这样的格式 2008-08-05 23:48:04 -0400 时)
if time_ago < 1 hour, 3 weeks, 1 day, etc.
execute me
end
# If "time_ago" is more than 1 hour past the current time
if time_ago < 1.hour.ago
execute_me
end
使用 a<
查看是否time_ago
早于1.hour.ago
,并>
查看是否time_ago
比 新1.hour.ago
正如 davidb 所提到的,您可以合并时间,然后执行以下操作:
(1.day + 3.hours + 2500.seconds).ago
您还可以执行小数秒,例如:
0.5.seconds.ago
没有.milliseconds.ago
,因此如果您需要毫秒精度,只需将其分解为小数秒即可。也就是说,1毫秒前是:
0.001.seconds.ago
放在.ago
几乎任何数字的末尾都会将该数字视为#of 秒。
你甚至可以在括号中使用分数:
(1/2.0).hour.ago # half hour ago
(1/4.0).year.ago # quarter year ago
注意:要使用分数,分子或分母都必须是浮点数,否则 Ruby 会自动将答案转换为整数,并抛开你的数学运算。
你的意思是…… 像这样?
if time_ago < Time.now-(1.days+1.hour+1.minute)
execute me
end