Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Use american date format as default in Ruby 1.9
# Date.parse() with Ruby 1.9 is now defaulting to the European date style where the format is DD/MM/YYYY, not MM/DD/YYYY
# patch it to use US format by default
if RUBY_VERSION >= '1.9'
class String
def to_date
if self.blank?
nil
elsif self =~ /(\d{1,2})\/(\d{1,2})\/(\d{4})/
::Date.civil($3.to_i, $1.to_i, $2.to_i)
else
::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
end
end
end
class ActiveRecord::ConnectionAdapters::Column
def self.fallback_string_to_date(string)
string.to_date
end
end
end
timscott commented Mar 6, 2012

There's a gem for this now: american_date. https://github.com/jeremyevans/ruby-american_date

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment