timedelta
date manipulation
Python programming
time conversion
coding tutorial

Convert a timedelta to days, hours and minutes

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

python
1from datetime import timedelta
2
3
4def split_timedelta(td: timedelta):
5    total_seconds = int(td.total_seconds())
6    days, remainder = divmod(total_seconds, 24 * 60 * 60)
7    hours, remainder = divmod(remainder, 60 * 60)
8    minutes, _ = divmod(remainder, 60)
9    return days, hours, minutes
10
11
12delta = timedelta(days=2, hours=5, minutes=17)
13print(split_timedelta(delta))

This prints:

text
(2, 5, 17)

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:

python
1from datetime import timedelta
2
3delta = timedelta(days=2, hours=5, minutes=17)
4
5days = delta.days
6hours = delta.seconds // 3600
7minutes = (delta.seconds % 3600) // 60
8
9print(days, hours, minutes)

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:

python
1from datetime import timedelta
2
3delta = timedelta(days=1, hours=3, minutes=4)
4days, hours, minutes = split_timedelta(delta)
5
6message = f"{days} days, {hours} hours, {minutes} minutes"
7print(message)

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:

python
1from datetime import timedelta
2
3delta = timedelta(hours=-1, minutes=-30)
4print(delta)
5print(delta.days, delta.seconds)

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:

python
1def split_with_sign(td: timedelta):
2    sign = -1 if td.total_seconds() < 0 else 1
3    total_seconds = abs(int(td.total_seconds()))
4    days, remainder = divmod(total_seconds, 86400)
5    hours, remainder = divmod(remainder, 3600)
6    minutes, _ = divmod(remainder, 60)
7    return sign, days, hours, minutes

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() plus divmod for a clear conversion to days, hours, and minutes.
  • 'td.days is safe, but td.seconds is 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.

Course illustration
Course illustration

All Rights Reserved.