Rails Date Format in Form Field

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:

  1. 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.

  2. 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 to edit that date, it will redisplay the date in the format of yyyy-mm-dd which is not what we want. We want rails to redisplay the date in the mm/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 of yyyy-mm-dd. Again: this is not what we want. We want that erb tag to automatically redisplay the date in the format of mm/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 the l 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 file date_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 your date 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



Leave a reply



Submit