Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

This project is archived and is in readonly mode.

Date Formatting requires a to_date conversion before format

#4793

In Rails 2.x you could format dates with a simple

model.created_at.to_s(:my_custom_style)

However, in Rails 3, it seems that you need to explicitly convert to a date first:

model.created_at.to_date.to_s(:my_custom_style)

This, to me, seems to be a bug, as I would expect date formatting to work in the same way as before (or is to_date an unclear requirement - it's certainly not really covered in the code: http://github.com/rails/rails/blob/master/activesupport/lib/active_...)

Reported by Neil Middleton · June 8th, 2010 @ 11:17 AM

State: invalid
Milestone: none
Assigned to: Neeraj Singh Neeraj Singh
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh

    It is working fine for me with rails edge. Can you try again with rails edge?

    Time::DATE_FORMATS[:dashy_format]                   = '%Y-%m-%d-%H-%m-%S'  # => 2007-10-23-17-23-23
    
    ruby-1.8.7-p249 > User.first.created_at.to_s(:dashy_format)
      User Load (0.3ms)  SELECT "users".* FROM "users" LIMIT 1
     => "2010-06-25-20-06-41"
    

    June 25th, 2010 @ 09:38 PM

  2. Neil Middleton
    Neil Middleton
    • Importance changed from to Low

    Tested against edge rails and can confirm Time::DATE_FORMATS works correctly as described above.

    However, when using Date::DATE_FORMATS it fails which suggests that the comments in the code linked above are either incorrect, or the code isn't working (or I'm doing something wrong, which indicates bad docs).

    Link to sample application showing the issue here: http://github.com/neilmiddleton/lh4793 See users#show.

    June 26th, 2010 @ 10:45 AM

  3. Neil Middleton
    Neil Middleton
    • Assigned user set to Neeraj Singh

    June 26th, 2010 @ 10:45 AM

  4. Neil Middleton
    Neil Middleton
    • No changes were found…

    June 26th, 2010 @ 10:45 AM

  5. Neeraj Singh
    Neeraj Singh

    Date::DATE_FORMATS is working for me.

    ActiveRecord::Schema.define(:version => 20100627025642) do
    
      create_table "users", :force => true do |t|
        t.string   "name"
        t.date     "dob"
        t.datetime "created_at"
        t.datetime "updated_at"
      end
    
    end
    class User < ActiveRecord::Base
    end
    
    User.create(:dob => Date.today)
    ruby-1.8.7-p249 > User.first.dob.to_s(:dashy)
      User Load (0.3ms)  SELECT "users".* FROM "users" LIMIT 1
     => "2010-06-26"
    

    June 27th, 2010 @ 04:02 AM

  6. Neil Middleton
    Neil Middleton

    So is it simply that DateTime columns don't use Date::DATE_FORMATS without a conversion first? This needs to be a little clearer (in fact all of date/time formatting could do with some docs...)

    June 27th, 2010 @ 09:45 AM

  7. Neeraj Singh
    Neeraj Singh
    • State changed from new to invalid

    That is true. DateTime columns use Time::DATE_FORMATS.

    June 27th, 2010 @ 01:19 PM