Issue when parsing string date from CSV in Jupyter notebook

Question:

I am trying to parse a string date from a CSV in a Jupyter notebook:

from datetime import datetime
print(datetime.strptime('Aug 28, 2022', 'MMM d, yyyy'))

but I get the following error:

ValueError: time data 'Aug 28, 2022' does not match format 'MMM d, yyyy'

What am I doing wrong?

Answers:

Use other format string (format codes reference):

from datetime import datetime

print(datetime.strptime("Aug 28, 2022", "%b %d, %Y"))

Prints:

2022-08-28 00:00:00
Answered By: Andrej Kesely

Use the following format

from datetime import datetime
print(datetime.strptime('Aug 28, 2022','%b %d, %Y'))
Answered By: rektifyer

Try this out

from datetime import datetime
print (datetime.strptime ('Aug 28, 2022', '%b %d, %Y'))

To know about basic formats,
https://www.geeksforgeeks.org/python-datetime-strptime-function/

Answered By: Selvaganapathy

The correct solution is:

datetime.strptime('Aug 28, 2022', '%b %d, %Y')

See https://www.geeksforgeeks.org/python-datetime-strptime-function/

Answered By: nikitira

Check this out:(Explanation with samples)

We use notation that starts with % in datatime.strptime

datetime.strptime("Aug 28, 2022", "%b %d, %Y") 

Thank you 🙂

Answered By: Ogabek