Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
830 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...