Overview
The DateRangeOverlap plugin extends Atemporal with powerful date range overlap detection capabilities. It provides methods to check if two date ranges intersect and retrieve the overlapping period, with support for timezone handling, boundary inclusion options, and caching for optimal performance.Installation
Using extend()
Using lazyLoad()
Types
DateRange
Represents a date range with start and end dates.OverlapResult
The result of an overlap detection operation.OverlapOptions
Configuration options for overlap detection.Factory Methods
checkDateRangeOverlap()
Checks if two date ranges overlap and returns the overlapping period.range1- First date rangerange2- Second date rangeoptions- Configuration options (optional)
OverlapResult - Object containing overlap status and overlapping range
Throws:
InvalidDateRangeError- If a range is invalid or dates cannot be parsedOverlapDetectionError- If overlap detection fails
Instance Methods
rangeOverlapsWith()
Checks if a date range overlaps with the current date instance (treated as a single-point range).range- The date range to check againstoptions- Configuration options (optional)
OverlapResult - Object containing overlap status
Throws:
InvalidDateRangeError- If the range is invalid or the instance is invalid
to()
Creates a date range from the current instance to another date.endDate- The end date of the range
DateRange - Object with start and end dates
Throws:
InvalidDateRangeError- If the current instance is invalid
Overlap Detection Behavior
Including Boundaries (Default)
By default, ranges are considered overlapping if they touch at boundaries:Excluding Boundaries
WithincludeBoundaries: false, ranges must truly overlap:
Complete Examples
Basic Overlap Detection
Vacation Conflict Checker
Room Booking System
Using Instance Methods
Timezone Handling
Performance Features
The plugin includes built-in LRU caching for improved performance:- Caches overlap detection results (200 entries)
- Cache keys include range values and all options
- Automatic cache invalidation for invalid ranges
- Registered with global cache coordinator
Cache Statistics
The cache is automatically registered with Atemporal’s global cache coordinator:Error Handling
InvalidDateRangeError
Thrown when a date range is invalid:OverlapDetectionError
Thrown when overlap detection fails unexpectedly:Notes
- All date inputs are converted to Date objects in the result
- Invalid dates throw
InvalidDateRangeErrorby default - Set
strictValidation: falseto allow ranges where start > end - The
includeBoundariesoption affects both overlap detection and overlap range calculation - Cached results are reused for identical inputs and options
- The plugin uses efficient comparison logic for optimal performance
- Overlapping ranges return the intersection as
overlapRange - Non-overlapping ranges return
overlapRange: null