I need to display in a text field a date with format "dd/mm/YYYY". I tried include the following in en.yml:
date:
formats:
default: "%d/%m/%Y"
and
<%= f.date_field :date, value: t(:date)%>
But does not works.
Also I tried add a file called date_format.rb in initializers
folder with the code:
Date::DATE_FORMATS[:default] = "%d/%m/%Y"
But nothings happens
How can I do this?
Note: I'm using Rails 4.
你应该试试
t.strftime("%d/%m/%Y")
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.
It's not t
(I18n.translate) you should use.
It's l
(I18n.localize) instead.
So just do <%= f.date_field :date, value: l(f.object.date)%>
<%= f.date_field :date, value: t.strftime("%m/%d/%Y")%>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.