Home > Back-end >  python string convert to datetime doesn't work
python string convert to datetime doesn't work

Time:10-28

I'm trying to convert a string, I got from my program, to datetime.dateime but it doesn't seem to work because I have the wrong format? The thing is, I have the right format... Here is some code:

from datetime import datetime
Timer = "27.10.2022 • 08:24:03"
Timer = Timer.replace(".", "/")
Timer = Timer.replace("•", "", 1)
Timer = Timer.replace(" ", "", 1)
print(Timer)
NewT = datetime.strptime(Timer, "%d/%m/%y %H:%M:%S")
print(NewT)
print(type(NewT))

Output is:

27/10/2022 08:24:03

ValueError: time data '27/10/2022 08:24:03' does not match format '%d/%m/%y %H:%M:%S'

I don't see where the Problem is because you can see the formatted string is the exact format.

Does anyone know what I am doing wrong?

CodePudding user response:

It does work, you have to use %Y instead of %y

%Y works with years like 2022, 2023

%y works with years like 22, 23

  • Related