如何将特定日期与今天进行比较,以了解特定日期是否大于今天感谢Harish。
答案 0 :(得分:11)
@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
O / P
@date is less than today's date
答案 1 :(得分:4)
date
对象具有future?
和past?
方法。这是我能想到的最佳方式。
答案 2 :(得分:3)
如果您正在使用日期对象,则只需按预期进行比较。
date1 > date2
答案 3 :(得分:0)
我个人会这样做:
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所做的事情。