In this article, we will learn about the solution to the problem statement given below.
Problem statement: We are given time, we need to convert seconds into hours & minutes to seconds.
There are three approaches as discussed below−
def convert(seconds): seconds = seconds % (24 * 3600) hour = seconds // 3600 seconds %= 3600 minutes = seconds // 60 seconds %= 60 return "%02d:%02d:%02d" % (hour, minutes, seconds) #formatting n = 23451 print(convert(n))
#using date-time module import datetime def convert(n): return str(datetime.timedelta(seconds = n)) n = 23451 print(convert(n))
#using time module import time def convert(seconds): return time.strftime("%H:%M:%S", time.gmtime(n)) n = 23451 print(convert(n))
In this article, we have learned about how we can convert seconds into hours, minutes and seconds.