Changing the default date and time format in Rails 4

后端 未结 3 559
天涯浪人
天涯浪人 2020-12-03 01:22

I was looking for a way to change the default date format in Rails 4.

相关标签:
3条回答
  • 2020-12-03 01:43

    Add this

    # Date
    Date::DATE_FORMATS[:default] = "%d/%m/%Y" 
    
    # Time
    Time::DATE_FORMATS[:default] = "%d/%m/%Y %H:%M" 
    

    to config/initializers/date_time.rb

    Then restart the server.

    0 讨论(0)
  • 2020-12-03 01:52

    If you use i18n conversion don't forget the l method before your dates in views!

    <%= l your_date %>
    
    0 讨论(0)
  • 2020-12-03 02:04

    Found a nice approach through the Rails Internationalization (I18n) API

    Data and time formats can be 'translated' by adding the format to the i18n configuration.

    config/locales/en.yml

    en:
      date:
        formats:
          default: "%d/%m/%Y"
      time:
        formats:
          default: "%d/%m/%Y %H:%M"
    

    Note: remember to not have tabs for the indent, like I did first time :)


    As mentioned by NoelProf in the comments

    To use i18n conversion don't forget the l (lower case L) before your date in views! For example: <%= l your_date %>

    You are invited to comment if you found other ways working well.

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