I have a rails form that displays a date in a text_field:
<%= form.text_field :check_in_date %>
The date is rendered as yyyy-mm-dd
I'm trying to figure out how to have it display as mm-dd-yyyy
I tried adding this config but it didn't work.
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!( :default => '%m/%d/%Y' )
You need to convert your string into Date object. For that, use Date#strptime . You can use Date#strftime to convert the Date object into preferred format.
Simple one time solution is to use :value
option within text_field
call instead of adding something to ActiveRecord::Base
or CoreExtensions
.
For example:
<%= f.text_field :some_date, value: @model_instance.some_date.strftime("%d-%m-%Y") %>
I added these to my initializers/time_formats.rb
and it works great:
# Default format for displaying dates and times Date::DATE_FORMATS[:default] = "%m/%d/%Y" Time::DATE_FORMATS[:default] = "%m/%d/%Y"
Using Ruby 1.9.3 and Rails 3.2.x
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