Skip to content
15 changes: 15 additions & 0 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,21 @@ A :class:`timedelta` object represents a duration, the difference between two
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)

Negative :class:`!timedelta` objects can display in a way that may be confusing. Here's a recipe for displaying both positive and negative `timedelta` objects in a more readable format:

.. code-block:: pycon

>>> def pretty_timedelta(td):
... if td.days >= 0:
... return str(td)
... return f'-({-td!s})'
...
>>> d = timedelta(hour=-1)
>>> d
-1 day, 23:00:00
>>> pretty_timedelta(d)
-(1:00:00)


Class attributes:

Expand Down
Loading