0
Group A

Start date: 1/1/2017
End date: 1/31/2017

Group B

Start date: 1/29/2017
End date: 2/4/2017

To find if date ranges(Group B dates fall in range within Group A dates) overlap or not, i can go with this:

If (GroupA.StartDate<= GroupB.EndDate && GroupB.StartDate<= GroupA.EndDate)
{
  //Date in range - overlaps.
}

How do i get the list of the dates that don't overlap and don't fall in the range?

In the above example, i want to get back efficiently

1/29 - Yes
1/30 - Yes
1/31- Yes
2/1 - No
2/2 - No
2/3-  No
2/4-  No

Thanks in advance.

Sharpeye500
  • 8,775
  • 25
  • 95
  • 143

1 Answers1

1

This will work.

Step 1: Create a class with your needed properties.

 public  class MyDate
    {

        public DateTime Date { get; set; }
        public bool IsValid { get; set; }
    }

Step 2:

 public List<MyDate> CheckDates(DateTime groupAStart, DateTime groupAEnd,
                                DateTime groupBStart, DateTime groupBEnd)
        {
            List<MyDate> myDates=new List<MyDate>();
            for (DateTime date = groupAStart; date <= groupAEnd; date = date.AddDays(1))
            {
                MyDate myDate = new MyDate();
                if (date > groupBEnd || groupBStart > date)
                {
                    myDate.Date = date;
                    myDate.IsValid = false;
                }
                if(myDate.Date> DateTime.MinValue)
                  myDates.Add(myDate);

            }
            return myDates;
        }

Test it:

            DateTime groupAStart = Convert.ToDateTime("1/29/2017");
            DateTime groupAEnd = Convert.ToDateTime("2/5/2017");
            DateTime groupBStart = Convert.ToDateTime("1/1/2017");
            DateTime groupBEnd= Convert.ToDateTime("1/31/2017");
            var results = CheckDates( groupAStart ,  groupAEnd ,  groupBStart ,  groupBEnd);
Sharpeye500
  • 8,775
  • 25
  • 95
  • 143