# Python Program to Convert Milliseconds to Minutes and Seconds

In Python, we have a built-in function int(), timedelta(), and divmod() that can be used to get the number in the form integer and is useful for converting Miliseconds to Minutes and seconds. The milliseconds are defined by the short duration of time. The milliseconds are equal to one-thousandth of the second. When 5000 milliseconds convert into minutes the result value is 0.08 minutes and 5 seconds. For example- The photographer clicks the picture and saves it in a gallery which takes some seconds that time is preferred to be milliseconds.

## Syntax

int()


The int() function accepts the parameter to convert the value into an integer.

timedelta()


This is an in-built method in Python that specifies the time duration between two times.

divmod(‘dividend’,1000)


This is an in-built method in Python that returns a tuple containing the quotient and the remainder when parameter1 (dividend) is divided by parameter2 i.e. 1000.

## Example 1

In the following example, we will start the program by storing the value of milliseconds in the variable ‘m_sec’. Then store the variable ‘m_sec’ divided by 1000 in the variable sec to find the value of the second. Next, divide the variable ‘sec’ by 60 to get the value of minute with the help of variable min. Moving ahead to find the remaining second by using the mod(%) operator in the variable ‘rem_sec’. Finally, print the result with the help of the variable ‘m_sec’, ‘min’, and ‘sec’.

m_sec = 2000000
sec = m_sec // 1000
min = sec // 60
rem_sec = sec % 60
print(f"{m_sec} milliseconds convert to {min} minutes and {sec} seconds")


## Output

2000000 milliseconds convert to 33 minutes and 2000 seconds


## Example 2

In the following example, we will start the program by storing the value of milliseconds in the variable named ‘mill_sec’. Then divide the mill_sec / 1000 to find the total number of seconds and store it in the variable ‘total_sec’. Next, we set the input function in the variables ‘min’ and ‘sec’ to get the value in integers. Finally, print the result with the help of the ‘mill_sec’, ‘sec’, and ‘min’.

mill_sec = 175060
total_sec = mill_sec / 1000
min = int(total_sec // 60)
sec = int(total_sec % 60)
print(f"The {mill_sec} milliseconds convert to {min} minutes and {sec} seconds ")


## Output

The 175060 milliseconds convert to 2 minutes and 55 seconds


## Example 3

In the following example, the timedelta method from the datetime module is used in this program to construct a timedelta object that represents the requested number of milliseconds. The seconds attribute on the timedelta object returns the total number of seconds that the object is used to represent. The number of minutes and seconds represented by the total number of seconds is then determined by using integer division (//) and the modulo operator (%).

from datetime import timedelta
def ans(milliseconds):
t = timedelta(milliseconds=milliseconds)
minutes = t.seconds // 60
seconds = t.seconds % 60
return minutes, seconds
milliseconds = 900000
minutes, seconds = ans(milliseconds)
print(f"{milliseconds} milliseconds is equal to {minutes} minutes and {seconds} seconds")


## Output

900000 milliseconds is equal to 15 minutes and 0 seconds


## Example 4

In the following example use the divmod function to perform integer division while also calculating the remainder. The divmod function returns a tuple containing the remainder and the result of integer division. The program initially calculates the number of seconds and remaining milliseconds from the input milliseconds using divmod. It then applies divmod to the determined number of seconds to calculate the number of minutes and remaining seconds.

def mil_convert(milliseconds):
seconds, milliseconds = divmod(milliseconds, 1000)
minutes, seconds = divmod(seconds, 60)
return minutes, seconds
milliseconds = 100000
minutes, seconds = mil_convert(milliseconds)
print(f"{milliseconds} milliseconds is equal to {minutes} minutes and {seconds} seconds")


## Output

100000 milliseconds is equal to 1 minutes and 40 seconds


## Conclusion

We saw the logic building of converting the millisecond into minutes and seconds in both examples. There is a similarity of logic in all examples but in example 2, we used the int() function to get the value of minutes and second in integers.

Updated on: 01-Jun-2023

2K+ Views 