Date Range Overlap And Merge
Detect overlapping date ranges, compute their intersection duration, and merge touching or overlapping ranges.
Code
export interface DateRange {
start: Date;
end: Date;
}
function normalize(range: DateRange): DateRange {
if (!(range.start instanceof Date) || !(range.end instanceof Date)) {
throw new TypeError("range bounds must be Date instances");
}
if (Number.isNaN(range.start.getTime()) || Number.isNaN(range.end.getTime())) {
throw new RangeError("range bounds must be valid dates");
}
return range.start <= range.end
? { start: range.start, end: range.end }
: { start: range.end, end: range.start };
}
export function rangesOverlap(left: DateRange, right: DateRange): boolean {
const a = normalize(left);
const b = normalize(right);
return a.start.getTime() <= b.end.getTime() && b.start.getTime() <= a.end.getTime();
}
export function overlapDurationMs(left: DateRange, right: DateRange): number {
const a = normalize(left);
const b = normalize(right);
const start = Math.max(a.start.getTime(), b.start.getTime());
const end = Math.min(a.end.getTime(), b.end.getTime());
return Math.max(0, end - start);
}
export function mergeDateRanges(ranges: DateRange[]): DateRange[] {
const sorted = ranges.map(normalize).sort((a, b) => a.start.getTime() - b.start.getTime());
const merged: DateRange[] = [];
for (const range of sorted) {
const last = merged[merged.length - 1];
if (last && range.start.getTime() <= last.end.getTime()) {
if (range.end.getTime() > last.end.getTime()) last.end = new Date(range.end);
} else {
merged.push({ start: new Date(range.start), end: new Date(range.end) });
}
}
return merged;
}How the snippet works
Date Range Overlap And Merge keeps everything in the functions normalize, rangesOverlap, overlapDurationMs and mergeDateRanges.
Iteration happens inside for (const range of sorted) — everything outside it runs once. A guard precedes the TypeError and RangeError: bad state stops the flow there instead of continuing.
Using it
The primary signature is export function rangesOverlap(left: DateRange, right: DateRange)
rangesOverlap is what another module would consume; nothing else here is load-bearing for callers.
Key statements
const sorted = ranges.map(normalize).sort((a, b) => a.start.getTime() - b.start.getTime());
return a.start.getTime() <= b.end.getTime() && b.start.getTime() <= a.end.getTime();
throw new TypeError("range bounds must be Date instances");
Interactive Tools for This Topic
Date Add/Subtract CalculatorAdd or subtract days, weeks, months, or years from a date, clamping month ends such as January 31 plus one month.Date Difference CalculatorCalculate the number of days, weeks, whole months, and years between two ISO dates, computed in UTC.SemVer Range CheckerCheck whether a semantic version satisfies an npm-style range such as caret, tilde, comparison sets, wildcards, or hyphen ranges.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- How are precondition violations signaled?
- The code throws `TypeError` and `RangeError`. Handle it at the call site or let it propagate deliberately.
- Does the runtime need any help from npm, pip, cargo, or the like?
- No — the package manager has nothing to do here; nothing is declared. The file defines `normalize`, `rangesOverlap`, `overlapDurationMs` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.