Crista23 Crista23 - 4 months ago 20
Python Question

Python parse datestring to date

I am trying to parse a datetime string to date in Python. The input value is of the form:

"February 19, 1989"


I have been trying so far

datetime.datetime.strptime("February 19, 1989", "%B %d, %y")


but I am always getting error. What is the right way to parse such a date? Thank you!

Answer

The following works (changed small case y to uppercase Y):

datetime.datetime.strptime("February 19, 1989", "%B %d, %Y")

The reason is that %y is for 99, 98 and such, while %Y is for full year. You can read the full documentation on this here.