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.

- Python program to Convert Kilometers to Miles and Vice Versa
- Python Programs for Beginners | Basic to Advanced Python Practice Programs for Beginners
- Python Program to Read Height in Centimeters and then Convert the Height to Feet and Inches

**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:

- Using mathematical formula
- Using time module in python
- Using datetime module in Python
- Using divmod function

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**:

- Python program to Convert Kilometers to Miles and Vice Versa – A Step-By-Step Approach
- Python Program to Convert Decimal to Binary, Octal, and Hexadecimal
- Python Program to Put Even and Odd elements in a List into Two Different Lists
- Python Program to Read Height in Centimeters and then Convert the Height to Feet and Inches
- Python Program to Read a String from the User and Append it into a File
- Python Program to Read a List of Words and Return the Length of the Longest Word
- Python Program to Accept Three Digits and Print all Possible Combinations from the Digits