Today at work I had to fix a form that allowed users to enter expiration dates in the past. Well the form was passing the dates to the controller as a simple string, when I tried to do a comparison on the string it obviously didn't work. So I had to convert the string into a valid format to compare with a Date object. I first thought I could create my Date object like this, but I got an error.>> Date.new("03/01/2011")
NoMethodError: undefined method `-' for "03/01/2011":StringAfter some Googling I found the strptime method, you can call it on a Date object, with the string version of the date as a parameter and it outputs a nice, clean Date object that you are able to use in your comparison. I did the following: date_entered = Date.strptime("{#{params[:subscription][:expires_date]}}", "{ %m/%d/%Y }")Then used the date_entered variable in my comparison, like this: if(date_entered < Date.today)
# DO SOMETHING HERE
end
NoMethodError: undefined method `-' for "03/01/2011":StringAfter some Googling I found the strptime method, you can call it on a Date object, with the string version of the date as a parameter and it outputs a nice, clean Date object that you are able to use in your comparison. I did the following: date_entered = Date.strptime("{#{params[:subscription][:expires_date]}}", "{ %m/%d/%Y }")Then used the date_entered variable in my comparison, like this: if(date_entered < Date.today)
# DO SOMETHING HERE
end