Practical guide

Count Unique Days Across Overlapping Date Ranges

For distinct calendar dates across several inclusive ranges, merge overlapping or adjacent ranges before summing their lengths. Count each merged range as end−start+1. A simple sum can double count shared days, while using the earliest start and latest end can wrongly include gaps.

Calendar users working with Gregorian dates and practical planning data · 4 minute read · Published

On this page

Define what one counted unit means

Suppose you are combining several activity periods into one list of covered dates. If two activities happen on the same date, that date should contribute once to a distinct-date count. Counting activities, staff-days, nights or elapsed hours is a different task.

Here every range includes both endpoints and every date between them. November 1 through November 4 therefore includes four dates. Start and end are Gregorian calendar dates, not timestamps. Weekends are included; a business-day calendar with closures needs additional filtering after the distinct set is known.

Write the convention before merging. For a half-open interval that excludes its end, first decide how to represent its included dates; do not mix that endpoint rule into an otherwise inclusive table.

Validate and sort the supplied ranges

  1. Check that each endpoint is a valid full date.
  2. Reject reversed endpoints or explicitly correct them with the source’s agreement; never repair silently.
  3. Sort ranges by start date and then by end date.
  4. Keep every original pair available so the merge can be traced.

Two equal endpoints describe one included date under this rule. Empty input describes zero dates. A pair such as November 9 to November 8 is not an empty period under the stated inclusive convention; it is invalid input needing a decision.

If the actual request is total activity-days, summing each activity period may be appropriate. Confirm distinct-date counting before removing overlaps that the requested measure intentionally counts twice.

Merge overlaps with a complete trace

Original inclusive ranges for November 2027
Input rangeLength on its ownMerge actionCovered range so far
1–4 November4 datesStart first range1–4 November
3–6 November4 datesOverlaps; extend the end1–6 November
8–9 November2 datesGap on November 7; keep separate1–6 and 8–9 November

On a narrow screen, scroll inside the table to read its remaining columns. Keyboard users can focus the table and use the arrow keys.

The naive lengths add to 10. November 3 and 4 appear in both of the first two ranges, so the distinct union has 8 dates: November 1, 2, 3, 4, 5, 6, 8 and 9. November 7 does not enter the union.

At each step, compare the next start with the current end. If the next start is no later than the day after that end, merge and keep the larger end. Otherwise finish the current range and start another. Adjacent inclusive ranges can merge because there is no missing date between them.

Count merged ranges and preserve genuine gaps

After merging, November 1–6 has 6 dates and November 8–9 has 2, giving 8. Taking only the earliest start and latest end would count November 1–9 as 9 and wrongly include November 7.

For a nested range, keep the larger end: November 2–3 inside November 1–6 adds no new dates and must not shorten the existing covered interval. For identical duplicate ranges, the result remains the length of one range.

Using integer date ordinals makes the comparison precise: for inclusive [a, b], the length is b − a + 1. Merge when the next start ≤ current end + 1. Keep the actual ordinal endpoints for calculation and convert back to dates for display; the ordinal system must be the same for every input.

Handle touching periods and other measures

Original small checks under inclusive endpoint rules
RangesDistinct datesReason
November 1–2 and 3–44Adjacent; no gap
November 1–6 and 2–36Nested; no additional dates
November 4–4 twice1Same date counted once
November 1–2 and 4–43November 3 remains a gap

On a narrow screen, scroll inside the table to read its remaining columns. Keyboard users can focus the table and use the arrow keys.

The merge method was checked against explicit date-set enumeration for the worked trace. It removes duplicate dates; it does not estimate hours of use, staffing effort or booking charges. Those measures need their own units and rules.

For weekday-only counts, build the union first, then apply the stated weekend/closure calendar to the dates in it. Filtering each input and summing separately can still double count shared included weekdays.

Two complete overlap decisions

Case 1

Periods November 1–4 and 3–6 have lengths 4 and 4. How many distinct covered dates are there?

Show answer and explanation for case 1

Six, spanning November 1 through 6.

November 3 and 4 are shared. Merge the two inclusive ranges into 1–6, then compute 6−1+1=6. Eight is the sum of period lengths, a different measure.

Case 2

A third period covers November 8–9. Can the answer be counted as November 1–9?

Show answer and explanation for case 2

No. The distinct total becomes 8 and November 7 stays excluded.

The merged output is 1–6 and 8–9, with lengths 6 and 2. The earliest-to-latest shortcut produces 9 by adding a date covered by no input period.

Sources and further reading