Python Glossary Русский

all Python 3datetimeterm

Days between dates

timedelta

The number of days between two dates, found by subtracting two date objects — the result is a timedelta.

Syntax

(d2 - d1).days

Subtracting two date objects always gives whole days, but subtracting datetimes puts only complete days into .days and pushes the leftover hours into .seconds — use total_seconds() for the real difference. Negative spans are normalised downwards, so a gap of minus two hours reports .days == -1. Do not turn this into years with .days // 365 either: leap years make the estimate drift.

Examples

from datetime import date
print((date(2024, 12, 31) - date(2024, 1, 1)).days)  # → 365
birth = date(2000, 6, 15)
print((date.today() - birth).days)  # → ? зависит от сегодняшней даты
new_year = date(date.today().year + 1, 1, 1)
print((new_year - date.today()).days, 'days until NY')  # → ? например 86 days until NY

See also

Open in the full glossarydocs.python.orgReport an inaccuracy