Changing default date format in Rails

When i use,

Date::DATE_FORMATS[:default] = “%m/%d/%Y”

in environment.rb file or any .rb file in config/initializers, works
fine Rails 3.0.1, but the same doesn’t work with Rails 3.0.4.

Please let me know if there is some other way to define default date
formats at application level or if you need any other information.

Thanks
Chaitanya

Sean Corbett wrote in post #913239:

In Rails3 you can run:

Date::DATE_FORMATS[:default] = “%m/%d/%Y”

skipping the ActiveSupport::CoreExtensions::…

see your installation of active support, eg:

activesupport-3.0.0.beta3/lib/active_support/core_ext/conversions.rb for
more info.

And what about helpers?
I would like to let users to enter date in the text field (faster then
3x select), so value for some_object.date_attribute
should in f.text_field(:date_attribute)
be “21.7.2011” (not “2011-07-21”).

I am using virtual atributes for now:

def date_attribut_to_s
if self.date_attribute.kind_of?(Date)
return self.date_attribute.strftime("%d.%m.%Y")
else
return self.date_attribute
end
end

def date_attribut_to_s=(maybe_date)
date=nil

if maybe_date.blank?
  self.date_attribute=nil
elsif mybe_date.kind_of?(Date)
  self.date_attribute=maybe_date
else
  begin
    #some more complex date parsing required?
    date = Date.strptime(maybe_date, "%d.%m.%Y")
  rescue
    self.errors.add(:date_attribute,"Invalid date")
  end
  self.date_attribute = date unless date.blank?

end
end

and use it in view as f.text_field(:date_attribute_to_s) .

Foton

Valentine B. wrote in post #989611:

Use Rails I18n for this.

In config/locales/en.yml:

en:
date:
formats:
default: “%m/%d/%Y”

In views:

<%= l some_model.date_field %>

On 16 August 2011 09:45, Petr M. [email protected] wrote:

return self.date_attribute.strftime(“%d.%m.%Y”)
elsif mybe_date.kind_of?(Date)
end

and use it in view as f.text_field(:date_attribute_to_s) .

So what is the question?

Colin