- How to convert seconds to Hours, Minutes, and Seconds in Python with timedelta class?
- Step 1: Import the timedelta class from the datetime module
- Step 2: Convert the given seconds into hh:mm:ss using timedelta
- The timedelta() class can accept the given seconds as keyword argument seconds , and return a datetime.timedelta object representing the given seconds into hh:mm:ss format.
- Output
- Example 2
- Output
- How to convert seconds to Hours, Minutes, and Seconds in Python with the time module?
- Example
- Output
- How to convert seconds to Hours, Minutes, and Seconds in Python with Naive logic?
- Seconds to the hour, minutes, and second conversion Logic
- Example
- How to Convert Hours, Minutes, and Seconds to seconds in Python
- Formula to convert hours, minutes, and seconds to seconds
- Example
- Conclusion
While Programming or writing script, we come across the situation where we need to convert the given seconds into readable hours:minutes: seconds format or vice versa.
This Python tutorial will teach you how to convert a given integer seconds value to hours, minutes, and seconds. And by the end of this tutorial, you will be able to write Python programs and logic for
- Conversion of seconds into hours, minutes, and seconds
- Conversion of Hours, minutes, and seconds into seconds.
So let’s Get started.
How to convert seconds to Hours, Minutes, and Seconds in Python with timedelta class?
To handle time and date data in Python, we have the inbuilt datetime module. The datetime module contains various subclasses and methods to manipulate, represent, and edit time in Python. In the datetime module, we also the timedelta class that can convert given integer seconds to readable hh:mm:ss format. To use timedelta in Python, follow the following steps
Step 1: Import the timedelta class from the datetime module
The datetime is an inbuilt Python module, to import it in our Python script, we can use the from and import keywords.
Step 2: Convert the given seconds into hh:mm:ss using timedelta
The timedelta() class can accept the given seconds as keyword argument seconds , and return a datetime.timedelta object representing the given seconds into hh:mm:ss format.
Output
Example 2
For the given seconds argument, the timedelta() class returns timedelta() object in hh:mm:ss format, which is not that readable. To display the conversion of seconds into hours, minutes, and seconds, we first have to convert the return result into a string using str() function, then display it correctly.
Output
How to convert seconds to Hours, Minutes, and Seconds in Python with the time module?
Python also has a time module that is specifically used to deal with time data in Python. The time module supports a gmtime() method that is used to convert given seconds to a full date and time from the epoch that is January 1, 1970, 00:00:00 (UTC). We can take the help of this method and convert the given seconds into hours, minutes, and seconds.
Example
Output
Note: The downside of time.gmtime() method, it can only convert given seconds upto 23 hours, 59 minutes, and 59 seconds. If the seconds are more than 86399 it will show some inappropriate results.
How to convert seconds to Hours, Minutes, and Seconds in Python with Naive logic?
You are giving a Python interview. The interviewer asks you to write a Python script that can convert given seconds into hours, minutes, and seconds without using any inbuilt module or function. In such a situation, you need to write a logic that can convert the seconds to the required output. This hardcoded logic technique is also known as naive logic or naive method.
Seconds to the hour, minutes, and second conversion Logic
Example
Output
How to Convert Hours, Minutes, and Seconds to seconds in Python
The conversion of hours, minutes, and seconds into total seconds is effortless, all we do is convert every individual entity to seconds and sum them. For example, we can convert the hour into seconds by multiplying the hour value by 3600 because there are 3600 seconds in an hour. Similarly, we can multiply the given minutes by 60 seconds by 1 and sum them together.
Formula to convert hours, minutes, and seconds to seconds
Example
Output
Conclusion
In this Python tutorial, you learned how to convert the given seconds into hours, minutes, and seconds. This tutorial discussed three different techniques you can use to perform the task, including using inbuilt modules and using a naive approach.
In this tutorial, we have also discussed how we can convert the given hours, minutes, and seconds back to total seconds, which is very easy compared to converting the seconds to hh:mm:ss.
If you have any suggestions regarding this article or any queries, please comment in the comments section. We will be appreciated your feedback.
People are also reading:
- Polymorphism in Python
- Python inheritance
- Python Programs to Print Pattern
- Basic Python Exercise
- Create files in Python
- Find the length of a List in Python
- Slice Lists/Arrays and Tuples in Python
- Shuffle list in Python using random.shuffle() function
- Python Compile Regex Pattern using re.compile()
- Python PostgreSQL Tutorial Using Psycopg2
Leave a Comment on this Post