Astrodude11 Astrodude11 - 3 months ago 10
MySQL Question

Convert datetime.timedelta Object to HH:MM

My database table has a column whose datatype is

datetime
, so when queried it returns
HH:MM:SS
. However, using Python it returns
(datetime.timedelta(0, 57360),)
.

This is obviously the time difference in seconds. How do I convert the returned object into a string in the format
HH:MM
?

Answer

That's how you can print the time:

import datetime

delta = datetime.timedelta(0, 57360)
sec = delta.seconds
hours = sec // 3600
minutes = (sec // 60) - (hours * 60)
print(hours, ':', minutes)

you can also print the time with seconds by

print(str(delta))