Using blocks in Rails custom date formats
Posted January 15th, 2009; 1 comments.
A not-so-well known feature of ActiveSupport is its ability to handle blocks as definitions for custom date and time formats. Here’s one I just added to a project to give nice short dates, stripping out any leading zeros:
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!( :shorty => lambda {|date| date.strftime("%m/%d/%y").gsub(/0?(\d+)\/0?(\d+)\/(\d+)/, '\1/\2/\3')} )
You can drop that in your environment.rb; a better option is to place it in a file inside config/initializers.
1 Response to “Using blocks in Rails custom date formats”
Sorry, comments are closed for this article.
Thanks for the little snippets.