Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 has not already passed?

like image 690
krn Avatar asked Dec 04 '10 22:12

krn


People also ask

How can I compare two dates?

In Java, two dates can be compared using the compareTo() method of Comparable interface. This method returns '0' if both the dates are equal, it returns a value "greater than 0" if date1 is after date2 and it returns a value "less than 0" if date1 is before date2.

Which method is used to compare two date values?

For comparing the two dates, we have used the compareTo() method. If both dates are equal it prints Both dates are equal. If date1 is greater than date2, it prints Date 1 comes after Date 2.

Can you compare two dates as strings?

To compare two dates, you can use either toString() or valueOf() . The toString() method converts the date into an ISO date string, and the valueOf() method converts the date into milliseconds since the epoch.


1 Answers

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

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

like image 104
David Sulc Avatar answered Oct 14 '22 18:10

David Sulc