Change Default Date Format in Ruby on Rails

Just a quick and simple hint!
 
If you need to set the default formatting of dates throughout your whole application in Ruby on Rails, just add the following line to your "<your application>/config/environment.rb"
 
 
my_date_formats = { :default => '%d/%m/%Y' } ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(my_date_formats) ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(my_date_formats)
 
Note 1: Rails uses this :default format when attempting to write dates do the database, so be sure that your DBMS is able to use this new format
Note 2: you will need to restart your server so the changes will be effective.
 

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options