Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这是我的 IRB 会议
1.9.2p290 :020 > Date.strptime("31-2-2010", "%d-%m-%Y") ArgumentError: invalid date
我已经给出了正确的值,但它正在返回ArgumentError。我在这里错过了什么吗?
ArgumentError
是的,你遗漏了一些东西: 二月从来没有 31 天,无论年份如何。
你给了它一个无效的日期。
注意Time.strptime("31-2-2010", "%d-%m-%Y")会给你2010-03-03 00:00:00 +0000.
Time.strptime("31-2-2010", "%d-%m-%Y")
2010-03-03 00:00:00 +0000
我Time.strptime("31-2-2010", "%d-%m-%Y").to_date用来避免无效的日期错误。
Time.strptime("31-2-2010", "%d-%m-%Y").to_date