Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3996312
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:15:51+00:00 2026-05-20T07:15:51+00:00

Given a DateRange I need to return a list of Start and EndDates that

  • 0

Given a DateRange I need to return a list of Start and EndDates that overlaps the given period.

what is the best way to do it? Thanks for your time in advance.

        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<BookedPeriod> bookedPeriods=new List<BookedPeriod>();
             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<OverlappedPeriod> myOverllappedPeriods = GetOverllapedPeriods(startDate, endDate, bookedPeriods);
          }

          public static List<OverlappedPeriod>GetOverllapedPeriods(DateTime startDate,DateTime endDate,List<BookedPeriod>bookedPeriods)
          {
             List<OverlappedPeriod>overlappedPeriods=new List<OverlappedPeriod>();

             // 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; }
       }

EDITED

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

I have booked a subscription to the gym that goes from 01 March 11 to 20 April 11

I go on holiday between 05 Feb 11 to 15 Mar 11

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

myAttempt:
static void Main()
{
//Holiday Period
var startDate = new DateTime(2011, 02, 5);
var endDate = new DateTime(2011, 3, 15);

             List<BookedPeriod> bookedPeriods = new List<BookedPeriod>();
             bookedPeriods.Add(new BookedPeriod { StartDate = new DateTime(2011, 02, 5), EndDate = new DateTime(2011, 4, 20) });

             List<OverlappedPeriod> overlappedPeriods=new List<OverlappedPeriod>();

             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

               */
           }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:15:51+00:00Added an answer on May 20, 2026 at 7:15 am

    Are you looking for dates that are completely within the provided period, or only partially?

    Completely within the range:

    var overlapped = 
        from period in bookedPeriods
        where period.StartDate >= startDate && period.EndDate <= endDate
        select new OverlappedPeriod { StartDate = period.StartDate, EndDate = period.EndDate };
    overlappedPeriods = overlapped.ToList();    
    

    Partially overlapping:

    var overlapped = 
        from period in bookedPeriods
        where (period.StartDate >= startDate && period.EndDate <= endDate)
            || (period.EndDate >= startDate && period.EndDate <= endDate)
            || (period.StartDate <= startDate && period.EndDate >= startDate)
            || (period.StartDate <= startDate && period.EndDate >= endDate)
        select new OverlappedPeriod 
        {
            StartDate = new DateTime(Math.Max(period.StartDate.Ticks, startDate.Ticks)),
            EndDate = new DateTime(Math.Min(period.EndDate.Ticks, endDate.Ticks))
        };
    
    overlappedPeriods = overlapped.ToList();    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need no generate a full daterange in JScript from a given Startdate to
Given List<LabEntity> selected = originalSettings.SelectedInstanceLabs; List<LabEntity> available = Presenter.GetLabs(dateRange); if (!firstLoad) { //Remove selected
I'm trying to calculate time blocks from given date range. Here's my attempt: <?php
Given that the web application doesn't have su privileges, I'd like to execute a
Given the jQuery dropdown plugin below. Is there a way to add a method
I need to findout a better and quick way to query MySQL table to
Sorry if SO is not the best place, but I have time-tracking enabled in
I have a method that takes in an id, a start date, and and
If I was to given a specific java.util.Date, how can I generate a start
Table Each row represents a video that was on air at particular time on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.