A timedelta object represents a duration, the difference between two dates or times.
|days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)|
All arguments are optional. Arguments may be ints, longs, or floats, and may be positive or negative.
Only days, seconds and microseconds are stored internally. Arguments are converted to those units:
and days, seconds and microseconds are then normalized so that the representation is unique, with
0 <= microseconds < 1000000
0 <= seconds < 3600*24(the number of seconds in one day)
-999999999 <= days <= 999999999
If any argument is a float and there are fractional microseconds, the fractional microseconds left over from all arguments are combined and their sum is rounded to the nearest microsecond. If no argument is a float, the conversion and normalization processes are exact (no information is lost).
If the normalized value of days lies outside the indicated range, OverflowError is raised.
Note that normalization of negative values may be surprising at first. For example,
>>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)
Class attributes are:
timedelta(days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999).
Note that, because of normalization,
-timedelta.max is not representable as
a timedelta object.
Instance attributes (read-only):
||Between -999999999 and 999999999 inclusive|
||Between 0 and 86399 inclusive|
||Between 0 and 999999 inclusive|
||Sum of t2 and t3. Afterwards t1-t2 == t3 and t1-t3 == t2 are true. (1)|
||Difference of t2 and t3. Afterwards t1 == t2 - t3 and t2 == t1 + t3 are true. (1)|
||Delta multiplied by an integer or long.
Afterwards t1 // i == t2 is true,
|In general, t1 * i == t1 * (i-1) + t1 is true. (1)|
||The floor is computed and the remainder (if any) is thrown away. (3)|
||Returns a timedelta object with the same value. (2)|
||equivalent to timedelta(-t1.days, -t1.seconds, -t1.microseconds), and to t1* -1. (1)(4)|
||equivalent to +t when |
In addition to the operations listed above timedelta objects support certain additions and subtractions with date and datetime objects (see below).
Comparisons of timedelta objects are supported with the
timedelta object representing the smaller duration considered
to be the smaller timedelta.
In order to stop mixed-type comparisons from falling back to the
default comparison by object address, when a timedelta object is
compared to an object of a different type, TypeError is
raised unless the comparison is
!=. The latter
cases return False or True, respectively.
timedelta objects are hashable (usable as dictionary keys),
support efficient pickling, and in Boolean contexts, a timedelta
object is considered to be true if and only if it isn't equal to
See About this document... for information on suggesting changes.