w3resource

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

Python Basic: Exercise-65 with Solution

Write a Python program that converts seconds into days, hours, minutes, and seconds.

Pictorial Presentation:

Convert seconds to day, hour, minutes and seconds

Sample Solution:

Python Code:

# Prompt the user to input a time duration in seconds and convert it to a float.
time = float(input("Input time in seconds: "))

# Calculate the number of full days in the given time duration.
day = time // (24 * 3600)
# Update the time variable to hold the remaining seconds after subtracting full days.
time = time % (24 * 3600)

# Calculate the number of full hours in the remaining time.
hour = time // 3600
# Update the time variable to hold the remaining seconds after subtracting full hours.
time %= 3600

# Calculate the number of full minutes in the remaining time.
minutes = time // 60
# Update the time variable to hold the remaining seconds after subtracting full minutes.
time %= 60

# The 'time' variable now represents the remaining seconds, which is the number of seconds.
seconds = time

# Print the time duration in the format "d:h:m:s".
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.

Python Code Editor:

 

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 Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/python-basic-exercise-65.php