Crista23 Crista23 - 1 year ago 143
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download