Ruby 1.87 vs 1.92 Date.parse

前端 未结 5 2000
甜味超标
甜味超标 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:33

    Per this bug report, the ability to parse mm/dd/yy dates was intentionally removed in 1.9. Ruby's creator, Yukihiro Matsumoto says:

    "dd/dd/dd" format itself is very culture dependent and ambiguous. It is yy/mm/dd in Japan (and other countries), mm/dd/yy in USA, dd/mm/yy in European countries, right? In some cases, you can tell them by accident, but we should not rely on luck in general cases. I believe that is the reason parsing this format is disabled in 1.9.

    As hansengel suggests, you can use Date.strptime instead.

提交回复
热议问题