Given an integer *n* (in seconds), convert it into hours, minutes and seconds.

**Examples:**

Input :12345Output :3:25:45Input :3600Output :1:00:00

**Approach #1 : ** Naive

This approach is simply a naive approach to get the hours, minutes and seconds by simple mathematical calculations.

`# Python Program to Convert seconds ` `# into hours, minutes and seconds ` ` ` `def` `convert(seconds): ` ` ` `seconds ` `=` `seconds ` `%` `(` `24` `*` `3600` `) ` ` ` `hour ` `=` `seconds ` `/` `/` `3600` ` ` `seconds ` `%` `=` `3600` ` ` `minutes ` `=` `seconds ` `/` `/` `60` ` ` `seconds ` `%` `=` `60` ` ` ` ` `return` `"%d:%02d:%02d"` `%` `(hour, minutes, seconds) ` ` ` `# Driver program ` `n ` `=` `12345` `print` `(convert(n)) ` |

*chevron_right*

*filter_none*

**Output:**

3:25:45

**Approach #2 : ** Alternate to the Naive approach

By using the `divmod()`

function, which does only a single division to produce both the quotient and the remainder, you can have the result very quickly with only two mathematical operations.

`# Python Program to Convert seconds ` `# into hours, minutes and seconds ` ` ` `def` `convert(seconds): ` ` ` `min` `, sec ` `=` `divmod` `(seconds, ` `60` `) ` ` ` `hour, ` `min` `=` `divmod` `(` `min` `, ` `60` `) ` ` ` `return` `"%d:%02d:%02d"` `%` `(hour, ` `min` `, sec) ` ` ` `# Driver program ` `n ` `=` `12345` `print` `(convert(n)) ` |

*chevron_right*

*filter_none*

**Output:**

3:25:45

**Approach #3 : ** Using `timedelta`

(Object of `datetime`

module)

Datetime module provides *timedelta *object which represents a duration, the difference between two dates or times. `datetime.timedelta `

can be used to represent seconds into hours, minutes and seconds format.

`# Python Program to Convert seconds ` `# into hours, minutes and seconds ` `import` `datetime ` ` ` `def` `convert(n): ` ` ` `return` `str` `(datetime.timedelta(seconds ` `=` `n)) ` ` ` `# Driver program ` `n ` `=` `12345` `print` `(convert(n)) ` |

*chevron_right*

*filter_none*

**Output:**

3:25:45

**Approach #4 : ** Using `time.strftime()`

`time.strftime()`

gives more control over formatting. The format and `time.gmtime()`

is passed as argument. *gmtime *is used to convert seconds to special tuple format that `strftime()`

requires.

`# Python Program to Convert seconds ` `# into hours, minutes and seconds ` ` ` `import` `time ` ` ` `def` `convert(seconds): ` ` ` `return` `time.strftime(` `"%H:%M:%S"` `, time.gmtime(n)) ` ` ` `# Driver program ` `n ` `=` `12345` `print` `(convert(n)) ` |

*chevron_right*

*filter_none*

**Output:**

03:25:45

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course.

## Recommended Posts:

- Converting seconds into days, hours, minutes and seconds
- Program for converting hours into minutes and seconds
- Program to find the time after K minutes from given time
- How to add hours to the current time in Python?
- Python program to convert Set into Tuple and Tuple into Set
- Python | Difference between two dates (in minutes) using datetime.timedelta() method
- Get Minutes from timestamp in Pandas-Python
- 30 minutes to machine learning
- Python | Pandas TimedeltaIndex.seconds
- Python | Pandas Timedelta.seconds
- Get Seconds from timestamp in Python-Pandas
- Python - Time Strings to Seconds in Tuple List
- PyQt5 - Calculator for Number of seconds You have Lived
- Pafy - Getting Length in seconds for each item of Playlist
- Python Program to Convert a list of multiple integers into a single integer
- Python | Convert a list of Tuples into Dictionary
- Python | Convert a list of characters into a string
- Python | Convert a nested list into a flat list
- Python | Convert two lists into a dictionary
- Python | Convert a list into a tuple

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.