Ruby 1.87 vs 1.92 Date.parse

前端 未结 5 2003
甜味超标
甜味超标 2021-02-01 12:56

In Ruby 1.87 I could do:

Date.parse (\"3/21/2011\")

Now in 1.9.2 I get:

ArgumentError: invalid date

<
5条回答
  •  借酒劲吻你
    2021-02-01 13:48

      class << self
        def parse_with_us_format(date, *args)
          if date =~ %r{^\d+/\d+/(\d+)$}
            Date.strptime date, "%m/%d/#{$1.length == 4 || args.first == false ? '%Y' : '%y'}"
          else
            parse_without_us_format(date, *args)
          end
        end
        alias_method_chain :parse, :us_format
      end
    

提交回复
热议问题