Default Date Format in Rails (Need it to be ddmmyyyy)

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

I've been working with a rails form all day and I was just randomly testing it and tried the date 25/12/2009 and it came up with a huge error.

It was at this point I realised that rails is set to american date mode (mm/dd/yyyy) instead of the UK style: dd/mm/yyyy.

How can I set rails to automatically deal with all dates in dd/mm/yyyy format?

回答1:

In your settings file: config/environment.rb"

my_date_formats = { :default => '%d/%m/%Y' }   ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(my_date_formats)   ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(my_date_formats)  

source: http://thedevelopercorner.blogspot.com/2009/03/change-default-date-format-in-ruby-on.html



回答2:

If you're using @Chris Ballance's solution note that Rails now modifies the Date class directly so you'll get an uninitialized constant ActiveSupport error with the solution.

See: uninitialized constant ActiveSupport::CoreExtensions

The updated argument:

my_date_formats = { :default => '%d/%m/%Y' }  Time::DATE_FORMATS.merge!(my_date_formats)  Date::DATE_FORMATS.merge!(my_date_formats) 


回答3:

You can change the date format using internationalisation (I18n)

Just add (or change) this in your config/locales/en.yml:

en:   date:     order:       - :day       - :month       - :year 


回答4:

You're looking more for something like this, although that solution still isn't too elegant.

http://source.mihelac.org/2006/9/13/parsing-european-date-format-in-ruby-rails



回答5:

Updated I18n...

date:   formats:     default: "%m/%d/%Y"     short: "%b %d"     long: "%B %d, %Y" 


回答6:

I solved this by adding getters and setters named mydatefield_formatted which explicitly do what I want, and use those everywhere. Probably a shopping list of reasons not to do this but I quite like it so I thought I'd leave it here.

app/models/mymodel.rb

class Mymodel 

lib/value_formatters.rb

module ValueFormatters   extend ActiveSupport::Concern    module ClassMethods      def add_value_formatters       columns.each do |column|         case column.type           when :datetime             define_method("#{column.name}_formatted") { General.format_datetime(self.read_attribute(column.name)) }             define_method("#{column.name}_formatted=") {|value| self.update_attribute(column.name, General.parse_datetime(value)) }           when :date             define_method("#{column.name}_formatted") { General.format_date(self.read_attribute(column.name)) }             define_method("#{column.name}_formatted=") {|value| self.update_attribute(column.name, General.parse_date(value)) }           when :boolean             define_method("#{column.name}_formatted") { General.format_boolean(self.read_attribute(column.name)) }             define_method("#{column.name}_formatted=") {|value| self.update_attribute(column.name, General.parse_boolean(value)) }           else             # do nothing         end unless self.class.respond_to?("#{column.name}_formatted")       end     end    end  end 

lib/general.rb

class General    def self.parse_date(value, end_of_day = false)     result = nil     begin       if value.present?         if value.length == 8           result = DateTime.strptime(value, '%d/%m/%y')           if result.year > Time.new.year + 1             result = DateTime.new(result.year - 100, result.month, result.day)           end         else           result = DateTime.strptime(value, '%d/%m/%Y')         end       end     rescue Exception=>e       #     end     result = result + 23.hours + 59.minutes + 59.seconds if result && end_of_day     result   end    def self.parse_datetime(value)     result = nil     begin       if value.present?         result = DateTime.strptime(value, '%d/%m/%Y %H:%M')         result = nil if result e       #     end     result   end    def self.format_date(value)     value.present? ? value.strftime('%d/%m/%Y') : ''   end    def self.format_datetime(value)     value.present? ? value.strftime('%d/%m/%Y at %H:%M') : ''   end    def self.format_boolean(value, default = nil)     value = default if value == nil     case value       when true         'Yes'       when false         'No'       else         'Unspecified'     end   end    def self.parse_boolean(value, default = false)     case value.to_s.downcase       when 'true'       when 'yes'       when '1'       when 'on'         true       when 'false'       when 'no'       when '0'       when 'off'         false       else         default     end   end  end 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!