Python Convert Time in Seconds to Days, Hours, Minutes, Seconds

Python convert seconds to hours, minutes, seconds; Through this tutorial, i am going to show you how to convert seconds to hours minutes seconds in python.

In this tutorial, i will write a python programs to convert time in seconds to days hours minutes seconds.

Python Convert Time in Seconds to Days, Hours, Minutes, Seconds

  • Python convert seconds to hours, minutes, seconds milliseconds
  • Python program to convert seconds to day, hour, minutes and seconds

Python convert seconds to hours, minutes, seconds milliseconds

  • Import the datetime object from datetime module.
  • Create an object by calling the now() function of datetime class.
  • Print the current hour, minute, second and microsecond using the object of datetime.now().
#Import module
from datetime import datetime
# create obj
td = datetime.now()
# printing the current date and time
print("Current date & time: ", td)
# extracting and printing the current
# hour, minute, second and microsecond
print("Current hour =", td.hour)
print("Current minute =", td.minute)
print("Current second =", td.second)
print("Current microsecond =", td.microsecond)

After executing the program, the output will be:

Current date & time:  2020-04-28 04:59:31.015466
Current hour = 4
Current minute = 59
Current second = 31
Current microsecond = 15466

Python program to convert seconds to day, hour, minutes and seconds

  • Get input seconds from user in program.
  • Convert seconds to day, hour, minutes and seconds using  this 60 * 60 * 24 , 60 * 60 , and 60  and store each result in a variable.
  • Print converted seconds to days, hours, and minutes.
# python program to convert seconds to day, hour, minutes and seconds
time = float(input("Input time in seconds: "))
# convert seconds to day, hour, minutes and seconds
day = time // (24 * 3600)
time = time % (24 * 3600)
hour = time // 3600
time %= 3600
minutes = time // 60
time %= 60
seconds = time
#print day, hour, minutes and seconds
print('Days', day)
print('Hours', hour)
print('Minutes', minutes)
print('Seconds', seconds)

After executing the program, the output will be:

Input time in seconds:  456123
Days 5.0
Hours 6.0
Minutes 42.0
Seconds 3.0

Recommended Python Tutorials

Leave a Comment