w3resource

Python: Convert seconds to day, hour, minutes and seconds

Python Basic: Exercise-65 with Solution

Write a Python program to convert seconds to day, hour, minutes and seconds.

Pictorial Presentation:

Convert seconds to day, hour, minutes and seconds

Sample Solution:-

Python Code:

time = float(input("Input time in seconds: "))
day = time // (24 * 3600)
time = time % (24 * 3600)
hour = time // 3600
time %= 3600
minutes = time // 60
time %= 60
seconds = time
print("d:h:m:s-> %d:%d:%d:%d" % (day, hour, minutes, seconds))

Sample Output:

Input time in seconds: 1234565                                                                                
d:h:m:s-> 14:6:56:5 

Flowchart:

Flowchart: Convert seconds to day, hour, minutes and seconds.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to get file creation and modification date/times.
Next: Write a Python program to calculate body mass index.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Try-catch-else construct:

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")