Friday, December 27, 2024
Google search engine
HomeLanguagesPython 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; In this tutorial, you should learn how to get and print the current day, hours, minutes, second, and millisecond in python. After that, you will learn how to convert seconds to days, hours, minutes, seconds, milliSecond.

.medrectangle-3-multi-320{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:auto!important;margin-right:auto!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:250px;padding:0;text-align:center!important;width:100%}

Because, if you have good knowledge about python DateTime module. So you can easily convert second to hours, minutes, milliseconds or Hours, to second, minutes, millisecond, or minutes to seconds, hours, or day to hours, minutes, seconds, millisecond, etc.

That’s why we would love to share with you how to get and print current hour, minute, second and microsecond.

1: 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

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

  • Take input seconds from user.
  • Convert seconds to day, hour, minutes and seconds.
  • Print result.
# 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 Programs

RELATED ARTICLES

Most Popular

Recent Comments