
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Converting seconds into days, hours, minutes and seconds in C++
- C++ Program for converting hours into minutes and seconds
- How to convert JavaScript seconds to minutes and 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 HH:MM:SS to seconds with JavaScript?
- How can we create a MySQL function to find out the duration of years, months, days, hours, minutes and seconds?
- MySQL query to convert timediff() to seconds?
- How to get the seconds and minutes between two Instant timestamps in Java
- How to convert time seconds to h:m:s format in Python?
- Remove Seconds/ Milliseconds from Date and convert to ISO String?
- How to get hour, minutes and seconds in android using offset time API class?
- What is the best way to convert seconds into (Hour:Minutes:Seconds:Milliseconds) time in C#?
- In MySQL, how can I convert a number of seconds into TIMESTAMP?
Advertisements