This project is archived and is in readonly mode.

#4793 ✓invalid
Neil Middleton

Date Formatting requires a to_date conversion before format

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

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_...)

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh June 25th, 2010 @ 09:38 PM

    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"
    
  • Neil Middleton

    Neil Middleton June 26th, 2010 @ 10:45 AM

    • 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.

  • Neil Middleton

    Neil Middleton June 26th, 2010 @ 10:45 AM

    • Assigned user set to “Neeraj Singh”
  • Neil Middleton

    Neil Middleton June 26th, 2010 @ 10:45 AM

    • no changes were found...
  • Neeraj Singh

    Neeraj Singh June 27th, 2010 @ 04:02 AM

    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"
    
  • Neil Middleton

    Neil Middleton June 27th, 2010 @ 09:45 AM

    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...)

  • Neeraj Singh

    Neeraj Singh June 27th, 2010 @ 01:19 PM

    • State changed from “new” to “invalid”

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages