ISO 8601 Dates in Ruby on Rails

Posted by Daniel Butler Thu, 13 Apr 2006 19:26:00 GMT

If you would like ISO 8601 time and date formatting by default in your Ruby on Rails application, add the following code to your environment.rb file:

ActiveSupport::CoreExtensions::Time::Conversions::DATE_  
  FORMATS.update(:default => '%Y-%m-%d %H:%M:%S')  
ActiveSupport::CoreExtensions::Date::Conversions::DATE_    
  FORMATS.update(:default => '%Y-%m-%d')  

(There is no space in the DATE_FORMATS token above. I had to add it to get the code to wrap; make sure you remove it.)

Wikipedia explains some of the benefits of using the ISO 8601 standard: “Dates and times are self-contained, and do not rely on any external context for their values. They are entirely numerical in representation, although certain specialized formats use roman letters to label fields. Every ISO date has exactly one possible unambiguous interpretation. These properties allow them to be truly international, without reliance on regional conventions, such as the names of the days of the week.” I also like them because they always display with a uniform width, and they are sortable as character fields, which comes in handy when you’ve added Dynamic HTML table sorting code into your application.

ISO 8601 Examples
ISO 8601 Time Examples

Posted in ,  | Tags , ,  | 1 comment

Sponsored Links

Sponsored Links

Comments

  1. Avatar Ben Murphy said 388 days later:

    Thanks! Just saved me half an hour!

(leave url/email »)

   Comment Markup Help Preview comment