Python Program to Convert Days to Hours Minutes Seconds
You can convert days to hours, minutes, and seconds in Python by multiplying days by 24 for hours, then by 60 for minutes, and again by 60 for seconds, like
hours = days * 24, minutes = hours * 60, and seconds = minutes * 60.Examples
Input1
Output1 day = 24 hours, 1440 minutes, 86400 seconds
Input3
Output3 days = 72 hours, 4320 minutes, 259200 seconds
Input0
Output0 day = 0 hours, 0 minutes, 0 seconds
How to Think About It
To convert days into hours, minutes, and seconds, first understand that 1 day has 24 hours, each hour has 60 minutes, and each minute has 60 seconds. So multiply the number of days by 24 to get hours, then multiply hours by 60 to get minutes, and finally multiply minutes by 60 to get seconds.
Algorithm
1
Get the number of days as input.2
Calculate hours by multiplying days by 24.3
Calculate minutes by multiplying hours by 60.4
Calculate seconds by multiplying minutes by 60.5
Display the results.Code
python
days = int(input("Enter number of days: ")) hours = days * 24 minutes = hours * 60 seconds = minutes * 60 print(f"{days} day{'s' if days != 1 else ''} = {hours} hours, {minutes} minutes, {seconds} seconds")
Output
Enter number of days: 3
3 days = 72 hours, 4320 minutes, 259200 seconds
Dry Run
Let's trace the input 3 days through the code
1
Input days
days = 3
2
Calculate hours
hours = 3 * 24 = 72
3
Calculate minutes
minutes = 72 * 60 = 4320
4
Calculate seconds
seconds = 4320 * 60 = 259200
5
Print result
"3 days = 72 hours, 4320 minutes, 259200 seconds"
| Variable | Value |
|---|---|
| days | 3 |
| hours | 72 |
| minutes | 4320 |
| seconds | 259200 |
Why This Works
Step 1: Convert days to hours
Since 1 day has 24 hours, multiply the number of days by 24 to get total hours.
Step 2: Convert hours to minutes
Each hour has 60 minutes, so multiply hours by 60 to get total minutes.
Step 3: Convert minutes to seconds
Each minute has 60 seconds, so multiply minutes by 60 to get total seconds.
Alternative Approaches
Using total seconds calculation first
python
days = int(input("Enter number of days: ")) total_seconds = days * 24 * 60 * 60 hours = total_seconds // 3600 minutes = (total_seconds % 3600) // 60 seconds = total_seconds % 60 print(f"{days} day{'s' if days != 1 else ''} = {hours} hours, {minutes} minutes, {seconds} seconds")
This method calculates total seconds first and then breaks it down, useful if you want to handle partial days or seconds.
Using datetime.timedelta for conversion
python
from datetime import timedelta days = int(input("Enter number of days: ")) time = timedelta(days=days) hours = time.total_seconds() // 3600 minutes = (time.total_seconds() % 3600) // 60 seconds = time.total_seconds() % 60 print(f"{days} day{'s' if days != 1 else ''} = {int(hours)} hours, {int(minutes)} minutes, {int(seconds)} seconds")
This uses Python's built-in timedelta for more robust time calculations.
Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of arithmetic operations regardless of input size, so it runs in constant time O(1).
Space Complexity
Only a few variables are used to store intermediate results, so space complexity is O(1).
Which Approach is Fastest?
All approaches run in constant time, but direct multiplication is simplest and fastest for this problem.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Direct multiplication | O(1) | O(1) | Simple and fast conversions |
| Total seconds then breakdown | O(1) | O(1) | Handling partial days or seconds |
| Using timedelta | O(1) | O(1) | Robust time calculations with built-in library |
Remember that 1 day = 24 hours, 1 hour = 60 minutes, and 1 minute = 60 seconds for easy conversions.
Beginners often forget to multiply by 24 first and try to convert days directly to minutes or seconds without proper steps.