Skip to main content

How to Count the Days Between Two Dates in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
(<later date> - <earlier date>).days
PartWhat it means
<later date> - <earlier date>Two datetime.date values. The result is a timedelta
.daysThe 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​

days_between.py
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)

The days_between.py script in a macOS-style code window: an ordered date of 2 November 2026 and a delivered date of 20 November 2026, their difference stored in gap, then gap, its type, gap.days and the reversed subtraction's days are printed.

  • delivered - ordered subtracts 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​

Terminal
uv run python days_between.py
Output
18 days, 0:00:00
timedelta
18 days
-18

The output in a terminal: 18 days, 0:00:00, then timedelta, then 18 days, then -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​

MistakeWhat happensDo this instead
Subtracting a datetime from a dateTypeError: unsupported operand type(s) for -: 'datetime.date' and 'datetime.datetime'Make both dates: my_datetime.date()
Reading .seconds for the gapIt's 0 for a gap of whole days: .seconds is only the part below one dayUse .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 17Subtract their .date() values to count calendar days

Troubleshooting​

SymptomLikely causeFix
A negative number of daysThe earlier date came first in the subtractionWrite 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​