GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Convert seconds to day, hour, minutes and seconds - w3resource

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

Last update on May 17 2025 05:43:26 (UTC/GMT +8 hours)


Seconds to DHMS Converter

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.


For more Practice: Solve these Related Problems:

  • Write a Python program to convert a given number of milliseconds into days, hours, minutes, and seconds.
  • Write a Python program to display the current system uptime in days, hours, minutes, and seconds.
  • Write a Python program to calculate the total seconds in a given number of years.
  • Write a Python program to convert a given number of nanoseconds into human-readable time.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

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

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.