Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ingeniarius/1205977 to your computer and use it in GitHub Desktop.
Save ingeniarius/1205977 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment