How to convert a str like this format ‘2020-01-01 00:00:00+00:00’ to datetime in python?

I need convert a set of strings that is in this format “2020-01-01 00:00:00+01:00”.

I try use this:

from datetime import datetime

date = datetime.strptime("2020-01-01 00:00:00+01:00", '%Y-%m-%d %H:%M:%S+%Z')

But i have this output:

>> ValueError: time data '2020-01-01 00:00:00+01:00' does not match format '%Y-%m-%d %H:%M:%S+%Z'

How can i convert this date?

Answer

You dont need to include the + of the timezone, and it’s a lowercase z

from datetime import datetime

date = datetime.strptime("2020-01-01 00:00:00+01:00", '%Y-%m-%d %H:%M:%S%z')

print(date)

Leave a Reply

Your email address will not be published. Required fields are marked *