python convert seconds to hours minutes seconds code example
Example 1: seconds to days python
def show_time(time):
time = int(time)
day = time // (24 * 3600)
time = time % (24 * 3600)
hour = time // 3600
time %= 3600
minutes = time // 60
time %= 60
seconds = time
if day != 0:
return "%dD %dH %dM %dS" % (day, hour, minutes, seconds)
elif day == 0 and hour != 0:
return "%dH %dM %dS" % (hour, minutes, seconds)
elif day == 0 and hour == 0 and minutes != 0:
return "%dM %dS" % (minutes, seconds)
else:
return "%dS" % (seconds)
print(show_time(12345))
Example 2: minutes to seconds python
>>> import datetime
>>> str(datetime.timedelta(seconds=666))
'0:11:06'
Example 3: python time seconds
import time
seconds = 1545925769.9618232
local_time = time.ctime(seconds)
print("Local time:", local_time)