Python timedelta.total_seconds() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
timedelta method

Definition

total_seconds() returns the whole duration of a timedelta in seconds, as a float: days, seconds, and microseconds combined. timedelta(days=1, hours=2).total_seconds() is 93600.0.

It is the right way to turn the difference of two datetimes into a number. The .seconds attribute is not the same: it only holds the part below one day (0 to 86399).

timedelta.total_seconds() is an instance method of datetime.timedelta: call it on a duration, such as (end - start).total_seconds(). Import the class with from datetime import timedelta when you build durations yourself.

Syntax

Call it with no arguments on a timedelta.

python
seconds = delta.total_seconds()
seconds = (end - start).total_seconds()
CallWhat it does
timedelta(days=1, hours=2).total_seconds()Returns 93600.0.
timedelta(milliseconds=1500).total_seconds()Returns 1.5.
timedelta(minutes=-75).total_seconds()Returns -4500.0.

It equals delta / timedelta(seconds=1). Divide by 60 or 3600 for minutes or hours.

Method signatures
python
timedelta.total_seconds()
# returns: float, days * 86400 + seconds + microseconds / 10**6

Parameters

total_seconds() takes no parameters.

Return Value

Return ValueDescription
floatThe signed length of the duration in seconds; negative for negative durations.

Python Examples

Example 1 — Convert Several Durations to Seconds

Mix days, hours, minutes, milliseconds, and a single microsecond.

python
from datetime import timedelta

print(timedelta(days=1, hours=2).total_seconds())
print(timedelta(minutes=90).total_seconds())
print(timedelta(milliseconds=1500).total_seconds())
print(timedelta(microseconds=1).total_seconds())

How It Works

  1. Days included. One day plus two hours is 86400 + 7200 = 93600 seconds.
  2. Always a float. Whole numbers still print with .0.
  3. Fractions kept. 1500 milliseconds is 1.5 seconds.
  4. Tiny values. One microsecond is 0.000001, which Python prints in scientific notation as 1e-06.

Example 2 — Do Not Confuse total_seconds() with .seconds

Subtract two datetimes more than a day apart and compare the attribute with the method.

python
from datetime import datetime

start = datetime(2024, 3, 14, 22, 15)
end = datetime(2024, 3, 16, 1, 45, 30)
gap = end - start

print(gap)
print(gap.seconds)
print(gap.total_seconds())
print(round(gap.total_seconds() / 3600, 2))

How It Works

  1. Normalized parts. The gap is stored as 1 day plus 3:30:30.
  2. .seconds is partial. It holds only the 12630 seconds after the full day.
  3. Whole duration. total_seconds() adds the day: 86400 + 12630 = 99030.0.
  4. Hours. Dividing by 3600 and using round() gives 27.51 hours.

Example 3 — Format Negative and Long Durations as H:MM:SS

str() of a negative timedelta is confusing. Build a clear label from total_seconds() instead.

python
from datetime import timedelta


def hms(delta):
    total = int(delta.total_seconds())
    sign = "-" if total < 0 else ""
    minutes, seconds = divmod(abs(total), 60)
    hours, minutes = divmod(minutes, 60)
    return f"{sign}{hours:02d}:{minutes:02d}:{seconds:02d}"


late = timedelta(minutes=-75)
print(late)
print(late.total_seconds())
print(hms(late), hms(timedelta(hours=26, seconds=5)))

How It Works

  1. Odd default text. Minus 75 minutes is stored as -1 day plus 22:45:00, and that is what print() shows.
  2. Signed seconds. total_seconds() gives the plain value -4500.0.
  3. Split with divmod. divmod() breaks the absolute value into hours, minutes, and seconds; the sign is added back in front.
  4. Hours above 24. The helper keeps counting hours, so 26 hours 5 seconds prints as 26:00:05.

Frequently Asked Questions

Call delta.total_seconds(). It returns a float such as 93600.0.
.seconds is only the part below one day (0 to 86399). total_seconds() is the whole duration including days and microseconds.
delta.total_seconds() / 60 for minutes and / 3600 for hours.
(end - start).total_seconds(). Both values must be naive, or both aware.
The end came before the start. Use abs() on the result, or subtract the other way round.

Next: datetime.tzname()

Return the time zone name, such as 'UTC' or 'UTC+05:30', or None.

tzname() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful