Skip to content

Instantly share code, notes, and snippets.

@ptzn
Created January 14, 2011 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ptzn/779859 to your computer and use it in GitHub Desktop.
Save ptzn/779859 to your computer and use it in GitHub Desktop.
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
Copy link

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