Convert a timedelta to days, hours and minutes
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A Python timedelta already stores a duration, but you often need to break it into human-readable parts such as days, hours, and minutes. The safest way is to use total_seconds() and divmod so the conversion logic is explicit and easy to maintain.
Break the Duration into Components
Here is a straightforward helper:
This prints:
divmod keeps the code clean because each step returns both the quotient and the leftover remainder.
Understand days and seconds
You can also work from the days and seconds attributes directly:
This works for many positive durations, but total_seconds() is often easier to reason about because it starts from the full duration instead of splitting across attributes manually.
That matters because timedelta.seconds is not "all seconds in the duration". It is only the leftover seconds after full days have been removed.
Format the Result for Display
Once you have the parts, formatting is simple:
If you want shorter output, you can customize the formatter to omit zero-valued parts.
For example, a duration of 0 days, 2 hours, and 0 minutes might be displayed as "2 hours" in a UI even though the internal split still contains all three components.
Watch Out for Negative Durations
Negative timedelta values are where many quick solutions become confusing. For example:
timedelta normalizes negative values internally, so the raw days and seconds fields can look surprising. If negative durations matter in your application, handle the sign separately:
That produces cleaner results for countdowns or elapsed-time displays.
Common Pitfalls
The most common mistake is using td.seconds as if it represented the full duration. It only represents the leftover seconds within the current day.
Another issue is forgetting that total_seconds() returns a float. If you only care about whole minutes, cast or round appropriately before splitting the value.
People also overlook negative timedelta normalization and then wonder why they see -1 day, 22:30:00 instead of a simple negative hour count.
Finally, decide whether you want to round minutes, truncate them, or include seconds. Different applications need different display rules.
If the result is going into reports or billing logic, document that rule explicitly so everyone interprets the duration the same way.
Summary
- Use
total_seconds()plusdivmodfor a clear conversion to days, hours, and minutes. - '
td.daysis safe, buttd.secondsis only the leftover part within the day.' - Format the converted pieces separately for human-readable output.
- Handle negative durations deliberately if your code deals with countdowns or elapsed offsets.
- Choose whether to truncate or round based on how precise the display needs to be.
Related reading
- Convert all strings in a list to integers
- Convert all strings in a list to integers
- Convert array of indices to one-hot encoded array in NumPy
- Convert bytes to a string in Python 3
- Convert bytes to int?
- Convert columns into rows with Pandas
- Convert columns to string in Pandas
- Convert DataFrame column type from string to datetime
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.