title: Find Gap Date Ranges from two Set of Date Ranges C#
body: <p>I have  Base date Ranges and Test Date Range. I need to get GAP between base and test meaning  Missing Date Ranges that are in Base but not in Test. what would be the best way to do this?</p>
  
  <pre><code>Base Date Ranges
  1/1/2012    1/10/2012
  1/11/2012   1/25/2012
  
  
  Test Date Ranges
  1/2/2012    1/7/2012
  1/8/2012    1/9/2012
  1/15/2012   1/30/2012
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>Missing Date Ranges that are in Base but not in Test 
  1/1/2012    1/2/2012
  1/7/2012    1/8/2012
  1/9/2012    1/10/2012
  1/11/2012   1/15/2012
  </code></pre>
  
  <p>Tried this so far</p>
  
  <pre><code>   static void Main(string[] args)
      {
  
  
          List&lt;DateRanges&gt; aDateRanges = new List&lt;DateRanges&gt;();
          List&lt;DateRanges&gt; bDateRanges = new List&lt;DateRanges&gt;();
  
          aDateRanges.Add(new DateRanges(new DateTime(2012, 1, 1), new DateTime(2012, 1, 10)));
          aDateRanges.Add(new DateRanges(new DateTime(2012, 1, 11), new DateTime(2012, 1, 25)));
  
  
          bDateRanges.Add(new DateRanges(new DateTime(2012, 1, 2), new DateTime(2012, 1, 7)));
          bDateRanges.Add(new DateRanges(new DateTime(2012, 1, 8), new DateTime(2012, 1, 9)));
          bDateRanges.Add(new DateRanges(new DateTime(2012, 1, 15), new DateTime(2012, 1, 30)));
  
          DisplayDateRanges(GetGaps(aDateRanges, bDateRanges, 0), "Final Gap Fill");
          Console.WriteLine("Completed");
          Console.Read();
      }
  
      public class DateRanges
      {
          public DateTime StartDate { get; set; }
          public DateTime EndDate { get; set; }
          public DateRanges(DateTime Start, DateTime End)
          {
              StartDate = Start;
              EndDate = End;
          }
      }
  
      public static void DisplayDateRanges(List&lt;DateRanges&gt; dateRanges, string title)
      {
          Console.WriteLine("************************{0}****************************", title);
          Console.WriteLine(Environment.NewLine + "New Recursion");
          foreach (DateRanges br in dateRanges)
          {
              Console.WriteLine("Start Date {0}   End Date {1}", br.StartDate, br.EndDate);
          }
  
      }
  
      public static List&lt;DateRanges&gt; GetGaps(List&lt;DateRanges&gt; aDateRanges, List&lt;DateRanges&gt; bDateRanges, int recursionlevel)
      {
          List&lt;DateRanges&gt; gapFill = new List&lt;DateRanges&gt;();
          List&lt;DateRanges&gt; gapFillTemp = new List&lt;DateRanges&gt;();
          List&lt;DateRanges&gt; bDateRangesTemp = new List&lt;DateRanges&gt;(bDateRanges);
          Console.WriteLine(Environment.NewLine + "+++++++++++++++++++++++++++++++++++++++++Recursion Level Id {0} +++++++++++++++++++++++++++++++++++++++++", recursionlevel);
          DisplayDateRanges(aDateRanges, " A Date Ranges ");
          DisplayDateRanges(bDateRanges, " B Date Ranges ");
  
          foreach (DateRanges br in bDateRanges)
          {
              if (br.StartDate == br.EndDate)
                  return gapFill;
              foreach (DateRanges ar in aDateRanges)
              {
                  if (ar.StartDate == ar.EndDate)
                      return gapFill;
                  if (br.StartDate == ar.StartDate &amp;&amp; br.EndDate == ar.EndDate)
                      continue;
                  else if (br.StartDate &gt;= ar.StartDate &amp;&amp; br.EndDate &lt;= ar.EndDate)
                  {
                      gapFillTemp.AddRange(GetGaps(new List&lt;DateRanges&gt; { new DateRanges(ar.StartDate, br.StartDate) }, bDateRangesTemp, recursionlevel + 1));
                      if (gapFillTemp.Count == 0)
                      {
                          //gapFillTemp.Add(new DateRanges(ar.StartDate, br.StartDate));
                      }
                      bDateRangesTemp.AddRange(gapFillTemp);
                      gapFill.AddRange(gapFillTemp);
                      gapFillTemp.Clear();
  
                      gapFillTemp.AddRange(GetGaps(new List&lt;DateRanges&gt; { new DateRanges(br.EndDate, ar.EndDate) }, bDateRangesTemp, recursionlevel + 1));
                      if (gapFillTemp.Count == 0)
                      {
                         // gapFillTemp.Add(new DateRanges(br.EndDate, ar.EndDate));
                      }
                      bDateRangesTemp.AddRange(gapFillTemp);
                      gapFill.AddRange(gapFillTemp);
                      gapFillTemp.Clear();
                  }
                  else if (br.StartDate &lt; ar.EndDate &amp;&amp; br.EndDate &gt;= ar.EndDate)
                  {
                      gapFillTemp.AddRange(GetGaps(new List&lt;DateRanges&gt; { new DateRanges(ar.StartDate, br.StartDate) }, bDateRangesTemp, recursionlevel + 1));
                      if (gapFillTemp.Count == 0)
                      {
                          //gapFillTemp.Add(new DateRanges(ar.StartDate, br.StartDate));
                      }
                      bDateRangesTemp.AddRange(gapFillTemp);
                      gapFill.AddRange(gapFillTemp);
                      gapFillTemp.Clear();
                  }
  
                  else if (ar.StartDate &gt;= br.StartDate &amp;&amp; ar.StartDate &lt; br.EndDate)
                  {
                      gapFillTemp.AddRange(GetGaps(new List&lt;DateRanges&gt; { new DateRanges(br.EndDate, ar.EndDate) }, bDateRangesTemp, recursionlevel + 1));
                      if (gapFillTemp.Count == 0)
                      {
                         // gapFillTemp.Add(new DateRanges(br.EndDate, ar.EndDate));
                      }
                      bDateRangesTemp.AddRange(gapFillTemp);
                      gapFill.AddRange(gapFillTemp);
                      gapFillTemp.Clear();
                  }
  
                  else if (ar.StartDate &gt;= br.StartDate &amp;&amp; ar.EndDate &lt;= br.EndDate)
                  {
                      //     AS----AE
                      //  BS----------BE           
                      //Do Nothing
  
                  }
                  else
                  {
                      if (AllowedToAdd(bDateRangesTemp, new DateRanges(ar.StartDate, ar.EndDate)))
                      {
                          bDateRangesTemp.Add(new DateRanges(ar.StartDate, ar.EndDate));
                          gapFill.Add(new DateRanges(ar.StartDate, ar.EndDate));
                      }
                  }
  
              }
  
  
  
          }
          return gapFill;
      }
  
  
      static bool AllowedToAdd(List&lt;DateRanges&gt; bDateRanges, DateRanges newItem)
      {
          return !bDateRanges.Any(m =&gt;
              (m.StartDate &lt; newItem.StartDate &amp;&amp;
               newItem.StartDate &lt; (m.EndDate))
              ||
              (m.StartDate &lt; (newItem.EndDate) &amp;&amp;
               (newItem.EndDate) &lt;= (m.EndDate))
              ||
              (newItem.StartDate &lt; m.StartDate &amp;&amp;
               m.StartDate &lt; (newItem.EndDate))
              ||
              (newItem.StartDate &lt; (m.EndDate) &amp;&amp;
               (m.EndDate) &lt;= (newItem.EndDate))
              );
      }
  </code></pre>