I am trying to convert time data which is displayed in hours minutes and seconds into standard time. I want to discard the seconds as well. I'm selecting datetime from mysql like this
time = connect("SELECT","""SELECT datTime FROM myTable WHERE ID = 1;""")
I then get only the time by doing this
time = time[0][0].time()
And I have tried various methods of converting the time into standard time minus the seconds such as
noSeconds = time.strftime("%H:%M")
timeNoSec = datetime.strptime(noSeconds, '%H:%M')
standardTime = noSeconds.strftime('%I:%M %p')
print(standardTime)
but I just get the following error
standardTime = noSeconds.strftime('%I:%M %p')
AttributeError: 'str' object has no attribute 'strftime'
CodePudding user response:
This is because the variable time is a string and not a time format, so none of the ways you tried work. To convert I found this:
import datetime
origin = '2022-01-11 15:51:00'
time_total = datetime.datetime.strptime(origin, "%Y-%m-%d %H:%M:%S")
timeNoSec = time_total.strftime("%H:%M")
print (timeNoSec)
15:51