11

I want to change default date format in Rails. Format should be y/m/d . I add following code into my environment.rb

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.
merge!(:default => '%Y/%m/%d')

But it didn't work. How can I change default format? I use rails 2.3.8 version

Rosh
  • 730
  • 2
  • 12
  • 32

2 Answers2

19

Add a file to config/initializers with the following code:

Date::DATE_FORMATS[:default]="%Y/%m/%d"
Time::DATE_FORMATS[:default]="%Y/%m/%d %H:%M"
dexter
  • 13,365
  • 5
  • 39
  • 56
  • 1
    Note, several posts elsewhere suggest that messing with these settings affects what is stored in the database; while it may have been true in earlier versions of Rails, as of 3.2, it seems OK. – Tom Harrison Mar 13 '12 at 18:18
  • Note: this can also be done in a before_filter in a controller if you let your users select their own date and time formats. – UrLicht Mar 15 '12 at 22:41
  • Note that since Ruby 1.9, the ruby dateformat is `%d/%m/%Y` and needs to be changed for these to fully work – toobulkeh Apr 14 '15 at 13:09
0

Put that in a file in the config/initializers and that should work.

Rishav Rastogi
  • 15,484
  • 3
  • 42
  • 47