Python Datetime TimeDelta
A datetime.timedelta object represents a duration of time.
Usage
A datetime.timedelta object is constructed as:
import datetime t1 = datetime.timedelta(days=3, seconds=2, milliseconds=1)
It is internally a tuple of days, seconds, and milliseconds.
Total_Seconds
Convert the stored days, seconds, and milliseconds into just seconds.
Equivalent to:
import datetime t1 = datetime.timedelta(days=3) seconds = t1 / datetime.timedelta(seconds=1)
Operations
datetime.timedelta objects can be used with the following operators and functions.
Operation |
Meaning |
t1 + t2 |
sum |
t1 - t2 |
difference |
t1 * i |
multiplication by integer |
t1 * f |
multiplication by float rounded to nearest even microsecond |
t1 / t2 |
division of t1 in terms of t2, returning a float |
t1 / n |
division by a number rounded to nearest even microsecond |
t1 // i |
floored division by an integer |
t1 // t2 |
floored division of t1 in terms of t2, returning an integer |
t1 % t2 |
the remainder of t1 from t1 // t2 |
q, r = divmod(t1, t2) |
q = t1 // t2 and r = t1 % t2 |
-t1 |
datetime.timedelta(-t1.days, -t1.seconds, -t1.microseconds) |
abs(t1) |
t if t >= 0 else -t |
str(t1) |
returns a string like [D day[s], ][H]H:MM:SS[.UUUUUU] |
repr(t1) |
returns a string constructor call |
Some of these operations are not safe from overflows.