开发者

Ruby on Rails: the best way to compare two dates?

开发者 https://www.devze.com 2023-01-28 17:42 出处:网络
I have a date variable, stored as a String in the \'YYYY-mm-dd\' format. What is the best way to check, if that date开发者_如何学Go has not already passed?Date.parse(\'2010-11-01\') < Date.today

I have a date variable, stored as a String in the 'YYYY-mm-dd' format. What is the best way to check, if that date开发者_如何学Go has not already passed?


Date.parse('2010-11-01') < Date.today

will return true if '2010-11-01' has already passed


You can also use past?

Date.parse("2012-11-01").past?


If you want to compare some attributes of dates, you can do this too:

Date.parse('2010-11-01').month < Date.today.month
Date.parse('2010-11-01').year < Date.today.year
Date.parse('2010-11-01').day < Date.today.day
0

精彩评论

暂无评论...
验证码 换一张
取 消