Different Ways to convert seconds into Hours and Minutes

Python program to convert seconds into day, hours, minutes and seconds

In this post, we will look at various Python techniques for converting seconds to hours and minutes.

Date and Time

Python, as a multipurpose language, can be used for a variety of reasons. Python includes a number of packages that help us with data manipulation tasks.

To achieve the same result in timestamp conversion, i.e. conversion of seconds to hours or minutes, numerous strategies might be considered.

Examples:

Input:

timeSeconds=30000

Output:

converting given time in seconds 30000 = 8 hrs 20 minutes

Different Ways to convert seconds into Hours and Minutes in Python

There are several ways to convert seconds into hours and minutes in python some of them are:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1: Using mathematical formula

We will write a generic function which converts the given time into hours and minutes

First, we convert the supplied seconds value to the 24 hour format.

timeseconds = timeseconds % (24*3600)

Furthermore, because one hour is equal to 3600 seconds and one minute is equal to 60 seconds, we use the below mathematical logic to convert seconds to hours and minutes.

timehour = timeseconds/3600

timeminutes = timeseconds//60

Below is the implementation:

def convertSec(timeseconds):
    secondValue = timeseconds % (24 * 3600)
    timeHour = secondValue // 3600
    secondValue %= 3600
    timeMinutes = secondValue // 60
    secondValue %= 60
    print("converting given time in seconds",
          timeseconds, "=", timeHour, "hrs", timeMinutes, "minutes")


# given time in seconds
timeseconds = 30000
# passing given time in seconds to convertSec function to convert it to minutes and hours
convertSec(timeseconds)

Output:

converting given time in seconds 30000 = 8 hrs 20 minutes

Method #2: Using time module in python

The Python time module includes time. By giving the format codes to the strftime() function, you can display the timestamp as a string in a certain format.

The function time.gmtime() is used to convert the value supplied to it into seconds. Furthermore, the time.strftime() function converts the value supplied from time.gmtime() into hours and minutes using the format codes provided.

Below is the implementation:

import time
givenseconds = 30000
#converting given time in seconds to hours and minutes
temp = time.gmtime(givenseconds)
resultantTime = time.strftime("%H:%M:%S", temp)
#printing the given time in seconds to hours and minutes
print("converting given time in seconds", givenseconds,
      "=", resultantTime)

Output:

converting given time in seconds 30000 = 08:20:00

Method #3: Using datetime module in Python

The Python datetime module has a number of built-in functions for manipulating dates and times. The date and time. The timedelta() function manipulates and represents data in the correct time format.

Below is the implementation:

import datetime
# given time in seconds
givenSeconds = 30000
#converting given time in seconds to hours and minutes
resultantTime = datetime.timedelta(seconds=givenSeconds)
# printing the given time in seconds to hours and minutes
print("converting given time in seconds", givenSeconds,
      "=", resultantTime)

Output:

converting given time in seconds 30000 = 8:20:00

Method #4:Using divmod function

You may get the result quickly with only two mathematical operations by using the divmod() function, which performs a single division to generate both the quotient and the remainder.

This is similar to method 1

Below is the implementation:

# given time in seconds
timeSeconds = 30000
# storing it in another variable
givenSeconds = timeSeconds
# converting it into minutes and seconds
timeMinutes, timeSeconds = divmod(timeSeconds, 60)
# converting it into hours and minutes
timeHour, timeMinutes = divmod(timeMinutes, 60)
# printing the given time in seconds to hours and minutes
print("converting given time in seconds", givenSeconds,
      "=", timeHour, "hrs", timeMinutes, "minutes")

Output:

converting given time in seconds 30000 = 8 hrs 20 minutes

Related Programs: