Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to gets the number of overlapping days between 2 ranges?

Tags:

date

c#

range

let me explain the situation first - let's say i have 4 dates: BS, BE, PS, PE (S for start, E for end). I need to know how many days are over lapping when given those dates. for example : BE-05.01 , BS-10.01, PS-03.01, PE-07.01 the result is: 3 (05.01, 06.01 and 07.01 are overlapping)

I wrote the following code but it seems to messy and i want to check if maybe there is a simpler way to do this:

private static double GetNumOfOverLappingDays(DateTime BS, DateTime BE, DateTime PS, DateTime PE)
    {
        //case 1:
        //                  |--- B ---|
        //                  |----P ---|

        //case 2:
        //                  |--- B ---|
        //                          | --- P --- |

        //case 3:
        //                  |--- B ---|
        //          | --- P ---- |

        //case 4:
        //                  |--- B ---|
        //                     | - P - |

        //case 5:
        //                  |--- B ---|
        //              | -------- P -------- |

        double days = -1;
        bool isNotOverLap = (BS > PE) || (PS > BE);

        if (isNotOverLap == false)
        {
            //case 1
            if (BS == PS && BS == PE)
            {
                days = (PE - PS).TotalDays;
            }
            //case 2
            else if (BE > PS && BE < PE)
            {
                days = (BE - PS).TotalDays;
            }
            //case 3
            else if (BS > PS && BS < PE)
            {
                days = (PE - BS).TotalDays;
            }
            //case 4
            else if (BS < PS && BE > PE)
            {
                days = (PE - PS).TotalDays;
            }
            //case 5
            else if (BS > PS && BE < PE)
            {
                days = (BE - PS).TotalDays;
            }
        }
        return days+1;
    }

thanks in advance for any assitance,

Amit

like image 238
Dardar Avatar asked Dec 30 '13 09:12

Dardar


2 Answers

Try this:

    private static double GetOverlappingDays(DateTime firstStart, DateTime firstEnd, DateTime secondStart, DateTime secondEnd)
    {
        DateTime maxStart = firstStart > secondStart ? firstStart : secondStart;
        DateTime minEnd = firstEnd < secondEnd ? firstEnd : secondEnd;
        TimeSpan interval = minEnd - maxStart;
        double returnValue = interval > TimeSpan.FromSeconds(0) ? interval.TotalDays : 0;
        return returnValue;
    }
like image 98
Boluc Papuccuoglu Avatar answered Oct 15 '22 07:10

Boluc Papuccuoglu


Try to use for loop:

DateTime d1 = new DateTime(2013,12,1),
            d2 = new DateTime(2013,12,14),
            d3 = new DateTime(2013,12,10),
            d4 = new DateTime(2013,12,20);
int count = 0;

for (var d = d1.Date; d <= d2.Date; d = d.AddDays(1))
{
      if (d >= d3.Date && d <= d4.Date)
           count++;
}

Console.WriteLine(count);

Notice that this code gives integer value. If you need value in hours this code will not fit. Also this is not the most efficient approach, but it's simple and works ok for small ranges.

like image 21
Tony Avatar answered Oct 15 '22 07:10

Tony