How to change default format of Date in Rails 3?

牧云@^-^@ 提交于 2019-12-01 09:18:30

问题


I'm having problem with the date format of rails. It seems to me that the default Date Format is dd/mm/yyyy. How am I gonna change it to something like mm/dd/yyyy?

I read somewhere that ActiveSupport gem can handle this but I don't how it works.


回答1:


Put following line in your enviroment.rb

Date::DATE_FORMATS.merge!(:default => "%m/%d/%Y")

and try

 Time.now.to_date.to_s

On you rails console




回答2:


You can add the file into the config/initializers folder having name i.e. (date_time_formats.rb). Simply add following line to the file.

Date::DATE_FORMATS[:default] = '%m/%d/%Y'

As per requirement you can change date format.



来源:https://stackoverflow.com/questions/5256510/how-to-change-default-format-of-date-in-rails-3

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