Say I have 2 date ranges.
These date ranges can be represented as time intervals.
I want to find a date range that falls in two time spans.
Range 1: 2/1/2011 - 8/1/2011 (timespan of 6 months) Range 2: 5/2/2011 - 5/28/2011 (timespan of 26 days)
therefore, in this case, the intersection will be 5/2 / 2011-5 / 28/2011, but the ranges can move in any direction (or not intersect at all, in which case I want the resulting time period to be length 0)
after all, I need calendar dates for the start / end of the resulting intersection interval (not just ticks / hours / days, etc.)
Is there an elegant way to do this in C # 3.0?
UPDATE
I took the StriplingWarriors code and created a method from it.
private static DateRange GetIntersectionRange(DateRange range1, DateRange range2) { var iRange = new DateRange(); iRange.From = range1.From < range2.From ? range2.From : range1.From; iRange.To = range1.To < range2.To ? range1.To : range2.To; if (iRange.From > iRange.To) iRange = null; return iRange; }
source share