Home Python Datetime Methods total_seconds() Python timedelta.total_seconds() 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.
seconds = delta.total_seconds()
seconds = (end - start).total_seconds() Call What 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 timedelta.total_seconds()
# returns: float, days * 86400 + seconds + microseconds / 10**6 Parameters total_seconds() takes no parameters.
Return Value Return Value Description float The signed length of the duration in seconds; negative for negative durations.
Python Examples 1. Convert durations 2. total_seconds() vs .seconds 3. Negative durations Example 1 — Convert Several Durations to Seconds Mix days, hours, minutes, milliseconds, and a single microsecond.
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 Days included. One day plus two hours is 86400 + 7200 = 93600 seconds.Always a float. Whole numbers still print with .0.Fractions kept. 1500 milliseconds is 1.5 seconds.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.
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)) 1 day, 3:30:30
12630
99030.0
27.51 How It Works Normalized parts. The gap is stored as 1 day plus 3:30:30..seconds is partial. It holds only the 12630 seconds after the full day.Whole duration. total_seconds() adds the day: 86400 + 12630 = 99030.0.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.
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))) -1 day, 22:45:00
-4500.0
-01:15:00 26:00:05 How It Works Odd default text. Minus 75 minutes is stored as -1 day plus 22:45:00, and that is what print() shows.Signed seconds. total_seconds() gives the plain value -4500.0.Split with divmod. divmod() breaks the absolute value into hours, minutes, and seconds; the sign is added back in front.Hours above 24. The helper keeps counting hours, so 26 hours 5 seconds prints as 26:00:05.Frequently Asked Questions How do I convert a timedelta to seconds in Python? Call delta.total_seconds(). It returns a float such as 93600.0.
What is the difference between timedelta.seconds and total_seconds()? .seconds is only the part below one day (0 to 86399). total_seconds() is the whole duration including days and microseconds.
How do I get a timedelta in minutes or hours? delta.total_seconds() / 60 for minutes and / 3600 for hours.
How do I get the number of seconds between two datetimes? (end - start).total_seconds(). Both values must be naive, or both aware.
Why is total_seconds() negative? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion