1

I've got 4 dates startDate, endDate, periodStartDate, periodEndDate

And I need a method to return true if the period denoted by the startDate and endDate overlap the period denoted by the periodStartDate and periodEndDate.

This is C# 3.5 so can't use tuples. like the suggested link

My brain doesn't seem to be working so can somebody please help me out.

Thanks!

mat-mcloughlin
  • 6,492
  • 12
  • 45
  • 62

4 Answers4

3
if (startDate<periodStartDate)//start date is older
{

   if (periodStartDate < endDate) //if end date is later than start of period
       return true;
   else return false; //
}
else //period start is older
{
    if (periodEndDate < startDate) //period ended before startdate
         return false;
    else
         return true;
}

just take care about where you want "<", and where "<="

edit (more elegant solution):

if (end1<start2) || (end2<start1) return false
else return true;
Sasa Ninkovic
  • 355
  • 3
  • 7
1

Assuming that the end dates will always be equal or later than the start dates:

return startDate <= periodEndDate;
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
  • What? What if startDate < endDate < periodStartDate < periodEndDate ? – Zecc Apr 18 '11 at 15:46
  • @Zecc - It would seem rather strange if there logic allowed the end dates to come before the start dates. – ChaosPandion Apr 18 '11 at 15:50
  • Period 2011-01-01 to 2011-02-01, Date 2010-01-01 to 2010-02-01. Your test will return true, but periods clearly dont overlap. – mathieu Apr 18 '11 at 16:06
1

You only need to check if the endDate is before periodStartDate or if the startDate is after the periodEndDate.
(Assuming startDate < endDate and periodStartDate < periodEndDate)

In code

if (endDate < periodStartDate || periodEndDate < startDate) return false;  
else return true;  

or in visual =)

startDate -------- endDate periodStartDate -------- periodEndDate

periodStartDate -------- periodEndDate startDate -------- endDate

Niklas
  • 13,005
  • 23
  • 79
  • 119
  • This is correct in the negative: if endDate <= periodStartDate, then {start->end} is before {periodStart->periodEnd}; if start >= periodStart, then {start->end} comes after {periodStart->periodEnd}. In all other cases they overlap. – Zecc Apr 18 '11 at 15:53
  • I guess it could be argued whether the dates overlap or not if they are equal =) – Niklas Apr 18 '11 at 15:55
1

I think this should do what you want.

 {
            if (startDate < periodStartDate)
            {
                if (endDate>=periodStartDate)
                {
                    return true;
                }
                else
                {
                    return false;
                }                             
            }
            else
            {
                if (startDate <= periodEndDate)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

This will catch the overlap even if one range complete encompasses the other.

Slotty
  • 174
  • 1
  • 9