Rails convert date format

date, ruby-on-rails

Solution

This is similar to Getting rails to accept European date format (dd/mm/yyyy)

In your model, create a setter method, where "my_date" is your database field.

def my_date=(val)
  Date.strptime(val, "%d/%m/%Y") if val.present?
end

Problem

I have a form input in my application that accepts dates in the format DD/MM/YYYY eg 02/05/2012 is 2nd May 2012. What is the best way for me to convert this into a suitable format to be added to the database through ActiveRecord? Is there a simply way of converting 02/05/2012 to 05/02/2012 before adding to the database?

Original source

Related problems