Date Range Overlap

Date Range Overlap

Only 2 conditions that overlap does not exist

1
2
                                                        |------ Date Range A ------|
|------ Date Range B ------|

or

1
2
|------ Date Range A ------|                                                        
                                                        |------ Date Range B ------|

That means start date A is later than end date B, or end data A is early than start date B.

Overlap exists if neither of them is true.

In rails we can create a scope to find all overlaps

1
2
3
scope :overlaps, -\>(start_date_, end_date_) do
where “((start_date \<= ?) and (end_date \>= ?))", end_date_, start_date_
end

reference:
http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap
http://baodad.blogspot.com.au/2014/06/date-range-overlap.html