将视图中的日期格式更改为dd / mm / yyyy

问题描述:

Ruby中的默认日期格式是yyyy-mm-dd,但是在视图中我需要将它们设置为dd / mm / yyyy

The default date format in Ruby is yyyy-mm-dd, but I needed them to be dd/mm/yyyy in the view

我已经写了date_format config / initializers 中的文件格式为:

I have wrote a date_format file in config/initializers as:

Time::DATE_FORMATS.merge!( :uk_format => '%m/%d/%Y')

无法工作,因为我需要调用 date.to_s(:uk_format)才能使用此格式。但是由于我使用的是<%= f.text_field:paymentDate%> 来显示日期,所以我不确定是否可以添加

This didn't work since I need to call date.to_s(:uk_format) in order to use this format. But since I'm using <%= f.text_field :paymentDate %> to display the date, I'm not sure I can add to_s to this case.

有人可以帮帮我吗?

编辑1

实际上date.to_s(:uk_format)也不起作用,怎么办我正确使用了初始化程序.....?

Actually date.to_s(:uk_format) doesn't work either, how do I use initializer properly.....?

请参阅:更改默认的Ruby时间格式

使我的答案与上述链接相适应指定的日期和格式:

Adapted my answer linked to above for the Date and format specified:

由于您使用的是Rails,请利用I18n支持: http://guides.rubyonrails.org/i18n.html#adding-date-time-格式

Since you're using Rails, take advantage of the I18n support: http://guides.rubyonrails.org/i18n.html#adding-date-time-formats

# config/locales/en.yml
en:
  date:
    formats:
      default: "%d/%m/%Y"

然后您可以呼叫 I18n.l Date.today 来获取格式化的日期。或者,您可以使用<%= l @ foo.created_at%>

Then you can call I18n.l Date.today to get out a formatted date. Or in your view, you can use <%= l @foo.created_at %>.