Python Date Difference Calculator
Generate datetime code for date subtraction.
How the Python Date Difference Calculator Works
Pick two dates. Copy the Python code and run it. It uses datetime.date values and subtracts the first date from the last one to find the gap in days.
Enter the start date
Enter the earlier date.
Enter the end date
Enter the later date.
Generate the snippet
The calculator writes code with date.fromisoformat().
Run the code
Run the snippet in Python 3.7 or later. The output is the day count.
How the Count Is Calculated
Python subtracts dates and returns a timedelta object.
from datetime import date
delta = date.fromisoformat(end) - date.fromisoformat(start)
days = delta.days- date
- Calendar date with no time or time zone.
- timedelta
- Duration object returned by subtraction.
- .days
- Whole days of the timedelta. Negative, if end is earlier.
Example: date(2026, 12, 25) − date(2026, 12, 4) returns timedelta(days=21). (d2 − d1).days returns 21.
Worked Examples
The table lists Python patterns for common date questions.
| Goal | Python code | Result |
|---|---|---|
| Days between dates | (d2 - d1).days | 21 |
| Add days | d1 + timedelta(days=30) | date(2027, 1, 3) |
| Business days | numpy.busday_count(d1, d2) | 15 |
| Weekday | d1.weekday() # Monday = 0 | 4 (Friday) |
| Years, months, days | relativedelta(d2, d1) # dateutil | relativedelta(days=+21) |
| Seconds between datetimes | (t2 - t1).total_seconds() | float |
What the Tool Measures
Generate Python code to count the days between two dates. Copy the code into your own script to run the same count.
Python’s datetime module handles leap years and month lengths. numpy.busday_count counts weekdays from the start date up to, but not including, the end date, so 4 to 25 December 2026 returns 15.
When to Use This Tool
There are 4 main uses of the Python Date Difference Calculator, including data analysis and scripts.
Data analysis
Analysts compute day gaps in pandas with (df.end - df.start).dt.days.
Automation scripts
Developers schedule reminders a set number of days ahead.
Coding interviews
Candidates practise date math with the standard library.
Teaching
Teachers show how timedelta works.