Custom Date Formatting in Ruby on Rails Posted on October 22nd, 2010
Ruby on Rails offers a couple different standardized date formats which can be really helpful. I'm personally a huge fan of :db
, I hate looking it up all the time. But you may be wondering how to create a custom date format. Easy enough, open and create a new file(edit if you already have it) config/initializers/time_formats.rb
. In this file we'll keep all of our custom date formats. Add the following code:
Time::DATE_FORMATS[:slashy_format] = "%m/%d/%Y" DATE::DATE_FORMATS[:slashy_format] = "%m/%d/%Y"
Restart your server and there you have it. This format can accessed via @something.date.to_formatted_s(:slasy_format)
There's not really much else to it.