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.
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
- Check that each endpoint is a valid full date.
- Reject reversed endpoints or explicitly correct them with the source’s agreement; never repair silently.
- Sort ranges by start date and then by end date.
- 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
| Input range | Length on its own | Merge action | Covered range so far |
|---|---|---|---|
| 1–4 November | 4 dates | Start first range | 1–4 November |
| 3–6 November | 4 dates | Overlaps; extend the end | 1–6 November |
| 8–9 November | 2 dates | Gap on November 7; keep separate | 1–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
| Ranges | Distinct dates | Reason |
|---|---|---|
| November 1–2 and 3–4 | 4 | Adjacent; no gap |
| November 1–6 and 2–3 | 6 | Nested; no additional dates |
| November 4–4 twice | 1 | Same date counted once |
| November 1–2 and 4–4 | 3 | November 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
- Python date objects — documented calendar and arithmeticProleptic Gregorian dates, valid month/day ranges, exact day differences, ISO calendar fields and one-based day-of-year definition. Only the complete date-objects section was used.