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?
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
For this specific format you can call DateTime.parse
:
DateTime.parse("02/05/2012") # => Thu, 02 May 2012 00:00:00 +0000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With