# Convert time to hours, minutes, and seconds in python

In this tutorial, our focus will be on time. But don’t fret, this won’t be a dull history lesson. Instead, we will explore various methods to convert time from seconds to hours, minutes, and seconds.

In the future, we will use the preferred format to refer to time, which includes hours, minutes, and seconds.

It will appear similar to.

```
2:46:40
```

Let’s allocate some ‘time’ for contemplating the current issue. Undoubtedly, Python offers remarkable modules to handle the conversion on our behalf. However, let’s attempt to compose our own program initially, prior to resorting to the pre-existing modules.

## Creating a personalized function that transforms time into hours, minutes, and seconds.

Before creating our own conversion function, it is necessary to consider the problem from a mathematical standpoint.

How can you transform seconds into the desired format?

You must obtain the value of hours, minutes, and seconds.

Assuming the time in seconds is less than or equal to the total number of seconds in a day, any excess will be divided by the total seconds in a day and the remainder will be taken.

Mathematically, this is shown as:

```
seconds = seconds % (24 * 3600)
```

The % operator provides the leftover value.

There are 24 hours in a day, and each hour has 3600 seconds (60 seconds times 60 minutes). So, 24 multiplied by 3600 is equal to the total number of seconds in a day.

We can move forward and determine the numerical value of hours based on the given number of seconds.

### 1. Obtain the value of the hour

In order to extract the hour value from seconds, we will utilize the floor division operator (//).

It gives back the whole number part of the result.

Since we require the number of hours, we’ll calculate it by dividing the total seconds (n) by the number of seconds in an hour (3600).

Mathematically, it can be expressed as:

```
hour = seconds // 3600
```

Next, we must compute the minutes.

### 2. Obtain the value for minutes.

In order to determine the value of minutes, we must initially divide the total number of seconds by 3600 and examine the remainder.

Mathematically, this can be expressed as:

```
seconds = seconds % 3600
```

To determine the minutes value from the previous outcome, we’ll once again employ the floor operator.

```
minutes = seconds // 60
```

Since a minute consists of sixty seconds, we round down the value of seconds by using 60.

Once we have computed the value for minutes, we can proceed to calculate the value of seconds in our desired format.

### “Retrieve the value of the seconds.”

In order to obtain the seconds value, we must once more divide the total number of seconds by 60 (the number of seconds in one minute) and keep the remainder.

In mathematical terms, the process can be represented in the following manner:

```
seconds = seconds % 60
```

This will provide us with the second value necessary for the format we prefer.

### 4. Code finalized

Let’s gather the aforementioned information into a Python function.

```
def convert_to_preferred_format(sec):
sec = sec % (24 * 3600)
hour = sec // 3600
sec %= 3600
min = sec // 60
sec %= 60
print("seconds value in hours:",hour)
print("seconds value in minutes:",min)
return "%02d:%02d:%02d" % (hour, min, sec)
n = 10000
print("Time in preferred format :-",convert(n))
```

Translation:

Result:

```
seconds value in hours: 2
seconds value in minutes: 46
Time in preferred format :- 02:46:40
```

## One possibility:

Utilizing the Time module

Now, we will examine an integrated module that allows us to convert seconds into our desired format using just a single line of code.

The time module establishes the beginning of time for a computer as January 1, 1970, 00:00:00 (UTC) in Unix systems, which may vary between different systems. This starting point, referred to as the epoch, is like day 0 and serves as a reference when converting seconds with the time module.

Use the code provided to display the epoch in your system.

```
time.gmtime(0)
```

To convert seconds into the desired format, utilize the given line of code.

```
time.strftime("%H:%M:%S", time.gmtime(n))
```

This code receives the input of time in seconds as variable ‘n’ and allows you to display the values of hours, minutes, and seconds individually.

Here is the full Python code:

```
import time
n=10000
time_format = time.strftime("%H:%M:%S", time.gmtime(n))
print("Time in preferred format :-",time_format)
```

Result:

```
Time in preferred format :- 02:46:40
```

In addition, the time module provides the choice to showcase additional details like the day, month, and year.

`%a` |
display abbreviated weekday name. |

`%A` |
display full weekday name. |

`%b` |
display abbreviated month name. |

`%B` |
display full month name. |

`%c` |
display the appropriate date and time representation. |

`%d` |
display day of the month as a decimal number [01,31]. |

How about we attempt to utilize %a and %b?

```
import time
n=100000000000
time_format = time.strftime("Day: %a, Time: %H:%M:%S, Month: %b", time.gmtime(n))
print("Time in preferred format :-",time_format)
```

Please provide the original sentence or phrase that you would like me to paraphrase natively.

```
Time in preferred format :- Day: Wed, Time: 09:46:40, Month: Nov
```

## Utilizing the Datetime module.

Another option is to utilize the timedelta function within the DateTime module to convert seconds into the desired format.

It shows the elapsed time since the epoch in days, hours, minutes, and seconds.

Here is a Python script utilizing the Datetime module to convert seconds into the desired format:

```
import datetime
n= 10000000
time_format = str(datetime.timedelta(seconds = n))
print("Time in preferred format :-",time_format)
```

Result:

```
Time in preferred format :- 115 days, 17:46:40
```

## In conclusion,

In this tutorial, we explored three distinct methods to transform seconds into hours, minutes, and seconds. In general, there are two distinct approaches to tackle this issue.

You have two options: either create your own function or utilize a built-in module. We initially opted to create our own function and subsequently explored the time and DateTime module.

More tutorials

JSON fundamentals(Opens in a new browser tab)

Adding a string to a Python variable(Opens in a new browser tab)

How can virtual machine image formats be converted?(Opens in a new browser tab)

Executing Java programs using the Exec Maven Plugin(Opens in a new browser tab)

error Attempting to install Java on a MacBook.(Opens in a new browser tab)