Custom date formats in Rails

Posted Posted by Wes in Blog     Comments No comments
Feb
26

I like to keep my date formats all together, instead of having .strftime() mixed into my code.

Create a file /config/initializers/custom_date_formats.rb

Load your formats into Time::DATE_FORMATS

Time::DATE_FORMATS[:ymd] = "%Y-%m-%d"

Use like so:

@christmas_post.created_at.to_s(:ymd)
# => "2009-12-25"

Post comment

about databasically

We live and work in Kansas City, USA.

We're passionate about helping small businesses succeed and want to help you use technology to get more done.

From server, desktop, network management to programming custom web applications in Ruby on Rails, we're here to lend a hand.

Contact us if you have any questions!