Rails change date format in form field attribute value
I guess you can specify the input value explicitely, but it not the best solution i think, since you need to do this for every date field
= f.date_field :valid_from, class: 'form-control', data: { provide: 'datepicker' }, value: f.object.valid_from.to_s
Set and Display Dates in rails via format: %m/%d/%Y
The best solution I have found is a two-step process:
Allow dates to save within the format of mm/dd/yyyy
Simply add in the american_date gem. And with that: when you enter into a text_field for a date the format of:
mm/dd/yyyy
(ex: 1/30/2016) it will save the correct date! This works wonderfully with the bootstrap-datepicker-rails gem because when you use the datepicker to pick a date: it loads into the textbox the date in this format.Display dates in the format of
mm/dd/yyyy
however, even with the american_date gem installed: rails still wants to display the date in the
yyyy-mm-dd
format.Example: within a text_field, such as:
<%= f.text_field :date %>
, when the user goes back toedit
that date, it will redisplay the date in the format ofyyyy-mm-dd
which is not what we want. We want rails to redisplay the date in themm/dd/yyyy
format.Another Example: if you have an erb tag, such as
<%= @blog.some_date %>
then yet again: rails will display the date in the format ofyyyy-mm-dd
. Again: this is not what we want. We want that erb tag to automatically redisplay the date in the format ofmm/dd/yyyy
To fix this: you have two options:
One option is to open up your
config/locals/en.yml
file and use the setup specified in the original question of this post. I personally do not like this setup because you then need to remember to use thel
method everytime you want to display a date within your app.The other option (which I like best) is to go to your
config/initializers
directory and make a new file. Name the file anything you want. I named the filedate_display_format.rb
. Then within there just put this line:Date::DATE_FORMATS[:default] = "%m/%d/%Y"
. Now: a date within your erb tags will display in the proper format, and when you go to edit yourdate
attribute within a form: with the text box the date will display in the proper format as well.
How to display in a text field a date with format dd/mm/YYYY ?
You can try localizing the date with:
l(date)
and to get the shorter version:
l(date, format: :short)
In case you need to be able to display and edit a date in a form, you could try simple_form or formtastic and then display the date field:
<%= f.input :date, as: :date %>
This should automatically convert your date in the default format for the current locale.
Rails Date Format in edit view
You could add date value manually if the form is edit.
=@booking.booking_date.striftime("%d-%m-%YYYY")
20-04-2014
edit.html.erb
<%= form_for @booking do |f| %>
<%= render 'common/form_errors', object: @booking %>
<%= f.label :booking_date, "Booking Date" %><br>
<%= f.text_field :booking_date, :class => :datepicker, size: 10, :value=>"#{f.object.booking_date.striftime("%d-%m-%YYYY") unless f.object.new_record?}" %>
Related Topics
What Does "+=" (Plus Equals) Mean
How to Calculate How Many Years Passed Since a Given Date in Ruby
Override Rails Helpers with Access to Original
Ruby: Split String at Character, Counting from the Right Side
In Ruby, Can You Perform String Interpolation on Data Read from a File
How to Match Full Words and Not Substrings in Ruby
Rspec --Init Not Working/ 'Mkd Ir': Invalid Argument - ./C: (Errno::Einval)
How to Install Ruby 2.0.0 Correctly on Ubuntu 12.04
How to Remove Installed Ri and Rdoc
Ruby (And Rails) Nested Module Syntax
How to Get a Particular Line from a File
How to Create a Hash in Ruby That Compares Strings, Ignoring Case
Faulty Ruby Compilation with Rvm: Getting 'Undefined Symbol: Rb_Digest_Md5_Init' While Running Racku