PythonMastery

Days between two dates in Python

Read two dates typed as YYYY-MM-DD, subtract them, and show the gap in days and in weeks plus days. Also counts working days.

Subtracting one date from another gives a timedelta, the length of time between them. Its .days attribute is the number of days.

The program

python
from datetime import date

first = date.fromisoformat(input("First date (YYYY-MM-DD): "))
second = date.fromisoformat(input("Second date (YYYY-MM-DD): "))

gap = abs((second - first).days)
weeks, days = divmod(gap, 7)

print(f"Days between: {gap}")
day_word = "day" if days == 1 else "days"
print(f"That is {weeks} weeks and {days} {day_word}.")

Typed in this example: 2026-10-22, 2026-12-25. Press Run to type your own.

output
First date (YYYY-MM-DD): 2026-10-22
Second date (YYYY-MM-DD): 2026-12-25
Days between: 64
That is 9 weeks and 1 day.

How it works

To handle a typing mistake without a crash, put the fromisoformat() call in a try/except ValueError loop, as in the input validation loop.

Working days only

weekday() returns 0 for Monday up to 6 for Sunday. Step through the dates one day at a time and count the days that are not Saturday or Sunday:

python
from datetime import date, timedelta


def working_days(start, end):
    count = 0
    day = start
    while day < end:
        if day.weekday() < 5:
            count += 1
        day += timedelta(days=1)
    return count


start = date(2026, 10, 22)
end = date(2026, 12, 25)
print(f"Working days from {start} up to {end}: {working_days(start, end)}")
output
Working days from 2026-10-22 up to 2026-12-25: 46

The loop counts start but not end, in the same way as the subtraction above. Public holidays are different in every country, so they are not counted here. To skip them, keep a set of holiday dates and check day not in holidays as well.

Learn the idea: Dates, Times, and Timezones

A longer explanation: How do I find the number of days between two dates in Python?