# 4 Ways to convert seconds into hours and minutes in Python # 4 Ways to convert seconds into hours and minutes in Python

Hey, folks! In this article, we will be having a look at the various techniques to convert seconds to hours and minutes using Python.

## Understanding date and time

Python, being a multi-purpose language, can be used for vivid purposes. Python contains various libraries to offer us ease for tasks related to the manipulation of data.

In the conversion of timestamp, i.e. conversion of seconds to hours or minutes, various different techniques can be considered to attain the same.

Let us now have a look at the different ways to convert seconds to hours, minutes, etc.

## Method 1: Defining a Python function to convert seconds to hours and minutes

Python function can be defined to convert the seconds value into hours, minutes, etc.

Initially, we convert the input seconds value according to the 24 hour format,

seconds = seconds % (24*3600)

Further, as 1 hour is equivalent to 3600 seconds and 1 minute is equivalent to 60 seconds, we follow the belo logic to convert seconds to hours and minutes,

hour = seconds//3600

min = seconds // 60

Example:

 `def` `conversion(sec):``   ``sec_value ``=` `sec ``%` `(``24` `*` `3600``)``   ``hour_value ``=` `sec_value ``/``/` `3600``   ``sec_value ``%``=` `3600``   ``min` `=` `sec_value ``/``/` `60``   ``sec_value ``%``=` `60``   ``print``(``"Converted sec value in hour:"``,hour_value)``   ``print``(``"Converted sec value in minutes:"``,``min``)``   ``sec ``=` `50000``conversion(sec)`

Output:

 `Converted sec value ``in` `hour: ``13``Converted sec value ``in` `minutes: ``53`

## Method 2: Python time module for the conversion of seconds to hours and minutes

Points of interest of Encapsulation:

Information Hiding: The client will have no clue about the inward usage of the class. It won’t be noticeable to the client that how the class is putting away qualities in the factors. He just realizes that we are passing the qualities to a setter technique and factors are getting instated with that esteem.

Expanded Flexibility: We can make the factors of the class as perused just or compose just relying upon our prerequisite. On the off chance that we wish to make the factors as perused just, at that point we need to overlook the setter strategies like setName(), setAge() and so on from the above program or in the event that we wish to make the factors as compose just, at that point we need to discard the get techniques like getName(), getAge() and so on from the above program

Reusability: Encapsulation likewise improves the re-ease of use and simple to change with new prerequisites.

Testing code is simple: Encapsulated code is anything but difficult to test for unit testing.

Example:

 `import` `time``sec ``=` `123455``ty_res ``=` `time.gmtime(sec)``res ``=` `time.strftime(``"%H:%M:%S"``,ty_res)``print``(res)`

Output:

 `10``:``17``:``35`

## Method 3: The Naive method

Example:

 `sec ``=` `50000``sec_value ``=` `sec ``%` `(``24` `*` `3600``)``hour_value ``=` `sec_value ``/``/` `3600``sec_value ``%``=` `3600``min_value ``=` `sec_value ``/``/` `60``sec_value ``%``=` `60``print``(``"Converted sec value in hour:"``,hour_value)``print``(``"Converted sec value in minutes:"``,min_value)``  `

Output:

 `Converted sec value ``in` `hour: ``13``Converted sec value ``in` `minutes: ``53`

## Method 4: Python datetime module to convert seconds into hours and minutes

Python datetime module has various in-built functions to manipulate date and time. The `datetime.timedelta() function` manipulates and represents the data in a proper time format.

Example:

 `import` `datetime``sec ``=` `123455``res ``=` `datetime.timedelta(seconds ``=``sec)``print``(res)`

Output:

 `1` `day, ``10``:``17``:``35`

## Conclusion

By this, we have come to the end of this topic. Feel free to comment below in case you come across any doubt.