Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

4 Ways to convert seconds into hours and minutes in Python

Conversion Of Seconds To Hour 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

Python time module contains time.strftime() function to display the timestamp as a string in a specified format by passing the format codes.

The time.gmtime() function is used to convert the value passed to the function into seconds. Further, time.strftime() function displays the value passed from time.gmtime() function into hours and minutes using the specified format codes.

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 = sec_value // 60
sec_value %= 60
print("Converted sec value in hour:",hour_value)
print("Converted sec value in minutes:",min)
   

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.

For more such posts related to Python, please do visit Python AskPython.


References