DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

How to Calculate the Number of Days Between Two Dates in Python

Use Python’s standard-library date subtraction for calendar-day differences, with separate handling for inclusive counts, timestamps, and time zones.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Subtract Python date objects and read the result’s .days attribute. For example, (date(2026, 10, 4) - date(2026, 10, 1)).days returns 3: the dates are three days apart, not four days inclusive.

Find the difference between calendar dates

For dates with no relevant time of day, use datetime.date. Subtracting one date from another produces a timedelta; its .days attribute gives the signed difference in whole calendar days. The Python documentation describes this behavior in its datetime reference.

As an Amazon Associate I earn from qualifying purchases.

from datetime import date

start = date.fromisoformat("2026-10-01")
end = date.fromisoformat("2026-10-04")

days_between = (end - start).days
print(days_between)  # 3

The order matters: end - start is positive if end is later, zero when both dates are the same, and negative if the dates are reversed. Ordinary subtraction measures the difference between date labels; it does not count both endpoints. For an inclusive count when the dates are ordered from earlier to later, add one: (end - start).days + 1.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Parse date strings before subtracting

ISO dates: YYYY-MM-DD

For strings such as 2026-10-01, date.fromisoformat() parses directly to a date:

from datetime import date

start = date.fromisoformat("2026-10-01")
end = date.fromisoformat("2026-10-04")
print((end - start).days)  # 3

Other known formats

Use datetime.strptime() with a format string that matches the input, then call .date() when only the calendar date matters. For example, for month/day/year strings:

from datetime import datetime

start = datetime.strptime("10/01/2026", "%m/%d/%Y").date()
end = datetime.strptime("10/04/2026", "%m/%d/%Y").date()
print((end - start).days)  # 3

Parsing makes the intended format explicit and rejects invalid calendar values. Avoid subtracting raw strings: their lexical order is not generally the same as chronological order unless the format guarantees it.

Use datetimes when time of day matters

If the question is about elapsed time between timestamps rather than the difference between calendar dates, use datetime objects. A timedelta can include sub-day components, so delta.days is only its normalized whole-day component—not a fractional total-days value. To express an elapsed duration as fractional days, divide by a one-day timedelta:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from datetime import datetime, timedelta

a = datetime(2026, 10, 1, 12, 0)
b = datetime(2026, 10, 2, 18, 0)
delta = b - a

fractional_days = delta / timedelta(days=1)
print(fractional_days)  # 1.25

Use delta.total_seconds() when you need a duration in seconds rather than days. The Python datetime documentation defines the timedelta fields and arithmetic.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle time zones and daylight-saving changes deliberately

Python does not allow subtraction between a naive datetime (one without time-zone information) and an aware datetime (one with it). If both values are aware but have different tzinfo attributes, subtraction follows the documented UTC-equivalent rule. If both aware values share the same tzinfo object, the documented arithmetic rule ignores time-zone adjustments. That distinction matters across daylight-saving transitions: a local calendar interval and elapsed time between instants may not be the same.

When the desired result is elapsed time between instants, convert both aware datetimes to UTC before subtracting. For named local zones, Python’s zoneinfo module provides IANA time-zone support; see the zoneinfo documentation for its behavior and daylight-saving examples. Decide whether your application needs calendar-date difference or elapsed UTC duration before choosing the calculation.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.