Django.fun

time data 'Fri, 03 Dec 2021 11:43:35' does not match format '%a, %b %Y %H:%M:%S'

I have the following string:"Fri, 03 Dec 2021 11:43:55". I want to convert it to datetime with python.
I am using strptime to convert it to datetime but it doesn't work.
Here is my code

from datetime import datetime

dte_str = "Fri, 03 Dec 2021 11:43:55"
dte = datetime.strptime(dte_str,"%a,%d %b %Y %H:%M:%S")

time data 'Fri, 03 Dec 2021 11:43:35' does not match format '%a, %b %Y %H:%M:%S' How to solve the problem please!

Answers: 1

Answered by Gedas Miksenas, Dec. 3, 2021, 3:35 p.m.

So this shows error because it's not actually the same format as your string:

dte = datetime.strptime(dte_str,"%a,%d %b %Y %H:%M:%S")

enter image description here

There needs to be a space after comma after Friday. Try this and it works.

dte = datetime.strptime(dte_str,"%a, %d %b %Y %H:%M:%S")

enter image description here