I’m looking for an extended answer to the question asked here:
Determine Whether Two Date Ranges Overlap
where any of the dates in either date range can be null. I’ve come up with the following solution, but I’m not sure if it can be simplified further.
(StartA == NULL || StartA <= EndB) &&
(EndA == NULL || EndA >= StartB) &&
(StartB == NULL || StartB <= EndA) &&
(EndB == NULL || EndB >= StartA)
Assuming:
DateTime ranges of StartA to EndA and StartB to EndB
EDIT: Sorry I quickly threw the above logic together, which seems to fail when either range’s start and end dates are NULL. See David’s solution below for a better & well-explained approach.
This case can be handled by a slight generalization of Charles Bretana’s excellent answer to that question.
In this case, assuming you want a null date to represent "no starting/ending bound," the conditions are modified. For
CondA, for instance, in order for DateRange A to be completely after DateRange B, DateRange A must have a defined starting time, DateRange B must have a defined ending time, and the starting time of A must be after the ending time of B:CondBis the same with A and B switched:Continuing,
and
I think this is actually a bit more robust than the solution you developed, because if
EndB == NULLbutStartAis not null, your first condition will wind up comparingStartA <= NULL. In most languages I’m familiar with, that’s an error condition.