How to display a date with the format "dd / mm / YYYY" in the text box?

I need to display a date in the text box with the format "dd / mm / YYYY". I tried to include the following in en.yml:

  date:
    formats:
      default: "%d/%m/%Y"

and

<%= f.date_field :date, value: t(:date)%>

But does not work.

I also tried adding a file called date_format.rb to the initializerscode folder :

Date::DATE_FORMATS[:default] = "%d/%m/%Y"

But nothing happens

How can i do this?

Note. I am using Rails 4.

+4
source share
4 answers

You can try to localize the date with:

l(date)

and get a shorter version:

l(date, format: :short)

, simple_form formtastic, :

<%= f.input :date, as: :date %>

.

+1

t.strftime("%d/%m/%Y")
+1

This is not t(I18n.translate) which you should use.

Instead l(I18n.localize).

So just <%= f.date_field :date, value: l(f.object.date)%>

0
source
<%= f.date_field :date, value: t.strftime("%m/%d/%Y")%>
-1
source

All Articles