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

后端 未结 2 1668
挽巷
挽巷 2020-12-17 21:39

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:

相关标签:
2条回答
  • 2020-12-17 22:32

    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'})
    
    0 讨论(0)
  • 2020-12-17 22:36

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

    0 讨论(0)
提交回复
热议问题