Home > Software design >  python string convert to datetime doesent work
python string convert to datetime doesent work

Time:10-28

Im trying to convert a string, I got from my programm, to datetime.dateime but it doesent seem to work because I have the wrong format? The thing ist, 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 dont see where the Problem is because you can see the formated string ist the exact format. Does anyone know what I am doing wrong? Thx for helping

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