最好的方法是检查日期是否超过一天或一年之久?检查日期是否超过一天,一岁多等?
7
A
回答
9
看到这个问题:comparision of date in ruby 的比较值像
1.day.ago
1.month.ago
1.year.ago
使用这些产生以下输出:
Loading development environment (Rails 3.0.7)
ruby-1.9.2-p180 :001 > 1.year.ago
=> Thu, 27 May 2010 17:45:25 UTC +00:00
ruby-1.9.2-p180 :002 > 1.month.ago
=> Wed, 27 Apr 2011 17:45:32 UTC +00:00
ruby-1.9.2-p180 :003 > 1.day.ago
=> Thu, 26 May 2011 17:45:36 UTC +00:00
ruby-1.9.2-p180 :004 >
,看看是否可行。
3
if the_date < 1.day.ago or the_date < 1.year.ago
1
尝试:
a_date > 1.day.ago
a_date > 1.year.ago
4
请使用ActiveSupport助手
@date < 1.day.ago
@some_other_date < 2.years.ago
小心点,最近的日期是早于日期,例如更大'1.day.ago> 1.month.ago' – 2015-03-27 07:56:26