1

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'

3
  • What do you mean by "standard time"? Do you mean the Unix time_t value, seconds since 1/1/1970, as returned by time.time()? The datetime module calls this a timestamp. Look for the totimestamp methods. Perhaps you should show an example of exactly what string you want. Commented Dec 26, 2021 at 22:37
  • Did you mean timeNoSec.strftime('%I:%M %p')? Or directly time[0][0].strftime('%I:%M %p')? Commented Dec 26, 2021 at 22:37
  • By standard time I mean something like 8:30 pm Commented Dec 27, 2021 at 0:25

1 Answer 1

0

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

From this post

Sign up to request clarification or add additional context in comments.

2 Comments

when i try this i get the following error ValueError: time data '2022-01-11 15:51:00' does not match format '%Y%m%d%H%M%S'
sorry, my mistake, you have to put the separation between the formatting, like this: strptime(time, "%Y-%m-%d %H:%M:%S") I will edit the post for better understanding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.