Date strptime returns an invalid date

Here is my IRB session

1.9.2p290 :020 > Date.strptime("31-2-2010", "%d-%m-%Y")
ArgumentError: invalid date

I gave the correct values, but it returns ArgumentError. Did I miss something?

+3
source share
2 answers

Yes, something is missing for you:
In February there are no 31 days, regardless of the year.

You have entered an invalid date.

+4
source

Please note that Time.strptime("31-2-2010", "%d-%m-%Y")will provide you 2010-03-03 00:00:00 +0000.

I use Time.strptime("31-2-2010", "%d-%m-%Y").to_dateto avoid invalid date errors.

0
source

All Articles