title: Getting Overlapping Start and End Date
body: <p>Given a DateRange I need to return a list of Start and EndDates that overlaps the given period.</p>
  
  <p>what is the best way to do it?  Thanks for your time in advance.</p>
  
  <pre><code>        static void Main(string[] args)
            {
               //Period 1stMarch to 20th April
               var startDate = new DateTime(2011, 03, 1);
               var endDate = new DateTime(2011, 4, 20); 
  
               List&lt;BookedPeriod&gt; bookedPeriods=new List&lt;BookedPeriod&gt;();
               bookedPeriods.Add(new BookedPeriod {StartDate = new DateTime(2011, 02, 5), EndDate = new DateTime(2011, 3, 15)});
               bookedPeriods.Add(new BookedPeriod { StartDate = new DateTime(2011, 03, 20), EndDate = new DateTime(2011, 4, 10) });
               bookedPeriods.Add(new BookedPeriod { StartDate = new DateTime(2011, 04, 01), EndDate = new DateTime(2011, 4, 15) });
  
               List&lt;OverlappedPeriod&gt; myOverllappedPeriods = GetOverllapedPeriods(startDate, endDate, bookedPeriods);
            }
  
            public static List&lt;OverlappedPeriod&gt;GetOverllapedPeriods(DateTime startDate,DateTime endDate,List&lt;BookedPeriod&gt;bookedPeriods)
            {
               List&lt;OverlappedPeriod&gt;overlappedPeriods=new List&lt;OverlappedPeriod&gt;();
  
               // Given a DateRange I need to return a list of Start and EndDates that overlaps
               //??how I do i
  
  
               return overlappedPeriods;
            }
         }
  
         public class BookedPeriod
         {
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
         }
  
          public class OverlappedPeriod
         {
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
         }
  </code></pre>
  
  <h1>EDITED</h1>
  
  <p>I have decided to edit in the hope of clarifing and therefore helping who has to help me.
  I get confused by overlapped vs intersect.
  A scenario might help</p>
  
  <p>I have booked a subscription to the gym that goes from 01 March 11 to 20 April 11</p>
  
  <p>I go on holiday between 05 Feb 11 to 15 Mar 11</p>
  
  <p>I need to get the start and end date when my subscription is valid that I will NOT BE GOING TO THE GYM
  The result I should get back is StartDate=1 March 2011 EndDate =15 March 2011</p>
  
  <p>myAttempt:
              static void Main()
                {
                   //Holiday Period
                   var startDate = new DateTime(2011, 02, 5);
                   var endDate = new DateTime(2011, 3, 15);</p>
  
  <pre><code>             List&lt;BookedPeriod&gt; bookedPeriods = new List&lt;BookedPeriod&gt;();
               bookedPeriods.Add(new BookedPeriod { StartDate = new DateTime(2011, 02, 5), EndDate = new DateTime(2011, 4, 20) });
  
               List&lt;OverlappedPeriod&gt; overlappedPeriods=new List&lt;OverlappedPeriod&gt;();
  
               foreach (var bookedPeriod in bookedPeriods)
               {
                  DateTime newStartDate = new DateTime();
                  DateTime newEndDate = new DateTime();
                  OverlappedPeriod overlappedPeriod=new OverlappedPeriod();
                  overlappedPeriod.StartDate = newStartDate;
                  overlappedPeriod.EndDate = newEndDate;
  
                  GetDateRange(bookedPeriod.StartDate, bookedPeriod.EndDate, out newStartDate, out newEndDate);
                  overlappedPeriods.Add(overlappedPeriod);
  
               }
               //do something with it
  
            }
             private static void GetDateRange(DateTime startDate,DateTime endDate,out DateTime newStartDate,out DateTime newEndDate)
             {
                /*
                 * I need to get the start and end date when my subscription is valid that I will NOT BE GOING TO THE GYM
                     The result I should get back is StartDate=1 March 2011 EndDate =15 March 2011
  
                 */
             }
  </code></pre>