Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5886 posts at DZone. View Full User Profile

RoR Date Formats

08.14.2006
Email
Views: 28537
  • submit to reddit
        From http://hittingthebuffers.com/2006/08/11/date-formats/:
August 11, 2006
Date formats

Here's a handy date related tip I just read when I was looking around for ways to make an RSS feed valid.

If you go to a command line and run script/console, then type ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS it displays a hash containing default date formatting which can be used with to_s() like this:
      xml.pubDate(article.created_at.to_s(:rfc822))

It's also possible to add additional formats to the end of application.rb:
      ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
        :default => "%m/%d/%Y",
        :date_time12 => "%m/%d/%Y %I:%M%p",
        :date_time24 => "%m/%d/%Y %H:%M"
      )

From Comments (as of 2006.08.14):
  2.
      My personal faves:
      :iso_8601_date_only => ‘%Y-%m-%d’,
      :iso_8601_time_only => ‘%H:%M:%S%z’
  Comment by Todd Sayre — August 12, 2006 @ 3:19 pm    

Comments

Snippets Manager replied on Mon, 2009/12/28 - 4:50am

We've written a new gem that handles date formatting quite nicely. Check it out at: http://github.com/platform45/easy_dates or for usage: http://blog.platform45.com/2009/12/27/easy-date-formatting-for-activerecord :)

Snippets Manager replied on Mon, 2012/05/07 - 2:26pm

If you use the Measure Map date slider here are a couple more you might find useful: :date_slider_raw => '%Y%m%dT00:00:00', :date_slider_clean => '%a. %B %d'

Snippets Manager replied on Wed, 2006/04/12 - 5:42pm

I get an error with that. What datatype are you using in the database.