How to Count the Days Between Two Dates in Python
How long did a delivery take? How many days until a deadline? In Python, the answer is a subtraction: one date minus another. The result is a timedelta, and its .days is the number you want. This guide counts the days between an order and its delivery, with real output from Python 3.15.2.
Syntax
(<later date> - <earlier date>).days
| Part | What it means |
|---|---|
<later date> - <earlier date> | Two datetime.date values. The result is a timedelta |
.days | The number of days in the timedelta, as an int |
Python's datetime documentation lists timedelta = date1 - date2 among the operations on dates, and notes: "This is exact, and cannot overflow."
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
from datetime import date
ordered = date(2026, 11, 2)
delivered = date(2026, 11, 20)
gap = delivered - ordered
print(gap)
print(type(gap).__name__)
print(gap.days, "days")
print((ordered - delivered).days)

delivered - orderedsubtracts the earlier date from the later one.type(gap).__name__shows what kind of value the subtraction made.- The last line subtracts the other way round.
Step 2: Run It
uv run python days_between.py
18 days, 0:00:00
timedelta
18 days
-18

The subtraction gave a timedelta of 18 days, which prints with a time part, 0:00:00. .days pulled out the plain number, 18. Subtracting the other way gave -18: the order of the two dates sets the sign.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Subtracting a datetime from a date | TypeError: unsupported operand type(s) for -: 'datetime.date' and 'datetime.datetime' | Make both dates: my_datetime.date() |
Reading .seconds for the gap | It's 0 for a gap of whole days: .seconds is only the part below one day | Use .days, or .total_seconds() for the full length |
Counting days between two datetimes with different times | .days counts whole days only: 2 Nov 18:00 to 20 Nov 09:00 gives 17 | Subtract their .date() values to count calendar days |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A negative number of days | The earlier date came first in the subtraction | Write later minus earlier |
FAQ
How do I get the gap in seconds?
gap.total_seconds(): for 18 days it's 1555200.0. The documentation warns that using .seconds for this "is a somewhat common bug".
Does this work with today's date?
Yes: (date.today() - ordered).days counts from today. The example uses fixed dates so its output stays the same.
Conclusion
delivered - ordered gave a timedelta of 18 days, and .days turned it into the number 18.
Additional Resources
- date objects — subtraction and comparison of dates.
- timedelta objects —
.days,.secondsandtotal_seconds().
