Change date format in the view to dd/mm/yyyy

那年仲夏 提交于 2019-11-30 08:24:03

问题


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

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

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

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.

Can someone help me out please.


Edit 1

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


回答1:


See: Change default Ruby Time format

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


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"

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




回答2:


Try this instead:

Time::DATE_FORMATS.merge!({:db => '%m/%d/%Y', :uk_format => '%m/%d/%Y'})
Date::DATE_FORMATS.merge!({:db => '%m/%d/%Y', :uk_format => '%m/%d/%Y'})


来源:https://stackoverflow.com/questions/5303495/change-date-format-in-the-view-to-dd-mm-yyyy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!