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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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