
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to convert seconds into hours, minutes and seconds
In this article, we will learn about the solution to the problem statement given below.
Problem statement: We are given time, we need to convert seconds into hours & minutes to seconds.
There are three approaches as discussed below−
Approach 1: The brute-force method
Example
def convert(seconds): seconds = seconds % (24 * 3600) hour = seconds // 3600 seconds %= 3600 minutes = seconds // 60 seconds %= 60 return "%02d:%02d:%02d" % (hour, minutes, seconds) #formatting n = 23451 print(convert(n))
Output
06:30:51
Approach 2: The datetime module
Example
#using date-time module import datetime def convert(n): return str(datetime.timedelta(seconds = n)) n = 23451 print(convert(n))
Output
6:30:51
Approach 3: THe time module
Example
#using time module import time def convert(seconds): return time.strftime("%H:%M:%S", time.gmtime(n)) n = 23451 print(convert(n))
Output
06:30:51
Conclusion
In this article, we have learned about how we can convert seconds into hours, minutes and seconds.
- Related Articles
- Convert 7290 seconds into hours and minutes.
- Converting seconds into days, hours, minutes and seconds in C++
- C++ Program for converting hours into minutes and seconds
- Convert 40 minutes into seconds.
- How to Convert Hours, Minutes and Seconds to Decimal Hours in Excel?
- How to convert JavaScript seconds to minutes and seconds?
- Add the following:2 hours 13 minutes 40 seconds and 4 hours 23 minutes 35 seconds.
- Subtract 4 hours 20 minutes 36 seconds from 8 hours 18 minutes 20 seconds.
- Golang program to convert milliseconds to minutes and seconds
- How to Convert Time to Decimal Hours /Minutes/ Seconds in Excel?
- Swift integer conversion to Hours/Minutes/Seconds
- Hours and minutes from number of seconds using JavaScript
- MySQL DateTime Now()+5 days/hours/minutes/seconds?
- Converting seconds in years days hours and minutes in JavaScript
- Convert:3 hours and 12 minutes into hours.

Advertisements