如何将特定日期与今天进行比较,以了解该特定日期是否大于今天
谢谢Harish。
如何将特定日期与今天进行比较,以了解该特定日期是否大于今天
谢谢Harish。
@date = "2010-07-20".to_date # 20th July
@today=Date.today # 21st July
if @date >= @today
puts "@date is greater than or equal to today's date"
else
puts "@date is less than today's date"
end
输出
@date is less than today's date
date
对象有future?
和past?
方法。这是我能想到的最好的方法。
date1 > date2
class Integer
def minutes
self * 60
end
def hours
self * 60.minutes
end
def days
self * 24.hours
end
def weeks
self * 7.days
end
def months #can probably be more accurate, but I'll just use 30.days
self * 30.days
end
def years
self * 12.months
def
end
如果我必须经常处理 Time
。现在你可以这样做
t1 = Time.now
t2 = t1 + 1.days
puts "You're in the past~" if t1 < t2
我假设这就是Rails的工作方式。