comparision of date in ruby
how to compare particular date with today to know whether that paricular date is gr开发者_StackOverfloweater than today
Thanks 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
O/P
@date is less than today's date
date
object has future?
and past?
methods. It's the best way I can think of.
If you're using date objects, you just compare as expected.
date1 > date2
I would personally do something like:
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
If I had to deal a lot with Time
s. Now you can do something like
t1 = Time.now
t2 = t1 + 1.days
puts "You're in the past~" if t1 < t2
I am assuming that's what Rails does anyway.
精彩评论