Topic: Date.strptime in rails 3.0.x

Hello,
I would like to convert a date in another format.
In my controller I get the date in dd/mm/yyyy format and I need to convert it in the yyyy/mm/dd format before to store it into the db.
I found a way to convert dates through the Date.strtime method butin rails 3.0.9 it isn't available.
How can I do that?

Is there another way to convert dates format?
Thanks to all

Re: Date.strptime in rails 3.0.x

I wouldn't change the date format when storing to the DB, but when you print it out in the view, just change it there.

So in your view you can do

<% @object.created_at.strftime("%Y/%m/%d") %>

That way it doesn't mess with the DB directly.

here are a few resources to use when doing date/time formatting
http://strfti.me/
http://www.wetware.co.nz/2009/07/rails- … -strftime/

Re: Date.strptime in rails 3.0.x

Thank you larzconwell for your answer but the date is an input parameter.
Let me better explain my situation.

In the form I expect the user to insert a date in the format dd/mm/yyyy.
When the date comes to the model (when the save method is called) it don't recognize a valid date and set it to Null in the database.
I solved this problem by creating a

Re: Date.strptime in rails 3.0.x

Oh okay I think I get it now!

Maybe this will help you
http://api.rubyonrails.org/classes/Acti … elper.html

Also in your model maybe you can validate the format with regex

Re: Date.strptime in rails 3.0.x

Also in case you need to know the regex (:

validates :attr, :format => {:with => /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/, :message => "The date must be in dd/mm/yyyy format."}