Customizing datetime format in en.yml in Rails 3

后端 未结 4 1822
北海茫月
北海茫月 2021-02-01 18:15

According to documentation at http://guides.rubyonrails.org/i18n.html#adding-date-time-formats, the best way to ask for specific formats for a date is to define them in /config/

4条回答
  •  生来不讨喜
    2021-02-01 19:10

    I suspect the problem here is in Rails' I18n:l function. Try with this:

    1. Add below lines to one of your initializer files, e.g., config/environment.rb:

      DateTime::DATE_FORMATS[:short]="short %Y-%m-%d %H:%M:%S"

      Time::DATE_FORMATS[:short] = "short %Y-%m-%d %H:%M:%S"

      Date::DATE_FORMATS[:short] = "short %Y-%m-%d"

    2. modify view:

      <%= item.create_date.to_s(:short) %>

    You can see output change to "short something".

    I know it's not a perfect answer, but hope it can give you some hints(I didn't have time to check the I18n::l function).

提交回复
热议问题