Python 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.

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *