-1

I have the following date string which I tried to extract the date from. I am getting an error as I am obviously not parsing it correctly.

from datetime import datetime

date_string = '2021-05-16T13:24:31+0000'
date_format = "%Y-%m-%d'T'%H:%M:%S'+0000'"
date_object = datetime.strptime(date_string, date_format)
print(date_object.date)

What would be the correct parsing format for this date string '2021-05-16T13:24:31+0000'?

Thank you.

Aivoric
  • 838
  • 2
  • 10
  • 24
  • What is the reason for the down vote? I haven't found an answer to this precise question on the site. And the question is very clear in my opinion. – Aivoric May 23 '21 at 15:34

1 Answers1

0

Those single quotes are messing with the date format, without them the code works fine:

from datetime import datetime

date_string = '2021-05-16T13:24:31+0000'
date_format = '%Y-%m-%dT%H:%M:%S+0000'
date_object = datetime.strptime(date_string, date_format)

print(date_object.strftime('%Y-%m-%d'))
Danillo Souza
  • 81
  • 1
  • 8