Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Convert Milliseconds to Minutes and Seconds
In Python, milliseconds can be converted to minutes and seconds using built-in functions like int(), timedelta(), and divmod(). Milliseconds represent short durations of time, where 1000 milliseconds equals 1 second. For example, 5000 milliseconds converts to 0 minutes and 5 seconds, or 125000 milliseconds converts to 2 minutes and 5 seconds.
Basic Conversion Formula
The conversion follows this logic:
- Total seconds = milliseconds ÷ 1000
- Minutes = total seconds ÷ 60
- Remaining seconds = total seconds % 60
Method 1: Using Integer Division
This approach uses floor division and modulo operators to extract minutes and seconds ?
milliseconds = 125000
# Convert to total seconds
total_seconds = milliseconds // 1000
# Extract minutes and remaining seconds
minutes = total_seconds // 60
remaining_seconds = total_seconds % 60
print(f"{milliseconds} milliseconds = {minutes} minutes and {remaining_seconds} seconds")
125000 milliseconds = 2 minutes and 5 seconds
Method 2: Using divmod() Function
The divmod() function returns both quotient and remainder in a single operation ?
def convert_milliseconds(milliseconds):
# Convert milliseconds to seconds and remaining milliseconds
total_seconds, remaining_ms = divmod(milliseconds, 1000)
# Convert seconds to minutes and remaining seconds
minutes, seconds = divmod(total_seconds, 60)
return minutes, seconds
milliseconds = 185000
minutes, seconds = convert_milliseconds(milliseconds)
print(f"{milliseconds} milliseconds = {minutes} minutes and {seconds} seconds")
185000 milliseconds = 3 minutes and 5 seconds
Method 3: Using timedelta from datetime
The timedelta class provides a cleaner approach for time calculations ?
from datetime import timedelta
def convert_with_timedelta(milliseconds):
# Create timedelta object
time_delta = timedelta(milliseconds=milliseconds)
# Extract minutes and seconds
total_seconds = int(time_delta.total_seconds())
minutes = total_seconds // 60
seconds = total_seconds % 60
return minutes, seconds
milliseconds = 245000
minutes, seconds = convert_with_timedelta(milliseconds)
print(f"{milliseconds} milliseconds = {minutes} minutes and {seconds} seconds")
245000 milliseconds = 4 minutes and 5 seconds
Comparison of Methods
| Method | Code Complexity | Best For |
|---|---|---|
| Integer Division | Simple | Basic conversions |
| divmod() | Clean | Multiple operations |
| timedelta | Readable | Complex time calculations |
Complete Example with User Input
Here's a practical example that accepts user input and handles different conversion methods ?
def convert_milliseconds_complete(ms):
"""Convert milliseconds to minutes and seconds using divmod"""
total_seconds, _ = divmod(ms, 1000)
minutes, seconds = divmod(total_seconds, 60)
return minutes, seconds
# Test with different values
test_values = [5000, 125000, 185000, 300000]
for ms in test_values:
minutes, seconds = convert_milliseconds_complete(ms)
print(f"{ms:6d} ms = {minutes:2d} minutes and {seconds:2d} seconds")
5000 ms = 0 minutes and 5 seconds 125000 ms = 2 minutes and 5 seconds 185000 ms = 3 minutes and 5 seconds 300000 ms = 5 minutes and 0 seconds
Conclusion
Converting milliseconds to minutes and seconds can be achieved using integer division, divmod(), or timedelta. The divmod() method is most efficient for this conversion, while timedelta offers better readability for complex time operations.
