Skip to content

Instantly share code, notes, and snippets.

@ptzn
Created January 14, 2011 16:47
Show Gist options
  • 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