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 6740345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:35:42+00:00 2026-05-26T11:35:42+00:00

ok so this should be simple really but seems I can’t get my head

  • 0

ok so this should be simple really but seems I can’t get my head around it.
I have a table called Dates which has a listing of all dates starting 2012 until its very end. Now when a customer makes reservation, I need to take starting date and ending date, generate the range between them in datetime format, and check each against its equivalent date in table to see capacity. if so, then decrement the capacity by booking requirement.
so far I have this code:

 private List<DateTime> getDateRange(DateTime startDate, DateTime endDate)
 {
        if (startDate > endDate)
        {
            return null;
        }
        List<DateTime> rv = new List<DateTime>();
        DateTime tmpDate = startDate;
        do
        {
            rv.Add(tmpDate);
            tmpDate = tmpDate.AddDays(1);
        } while (tmpDate <= endDate);
        return rv;
    }

The code is supposed to generate a list with dates in range and is called later in this function:

public void checkDateRange(DateTime startDate, DateTime endDate)
{

        //need to check each element in the list against the table DateTest:
        //changing the parameters of parse will give error. Require type handling
        //First, extract the elements:
        DateTime StartingDate = DateTime.Parse("02/25/2007");
        DateTime EndingDate = DateTime.Parse("03/06/2007");
        foreach (DateTime date in getDateRange(StartingDate, EndingDate))
        {
            date.ToShortDateString();
        }
          //rest of code should go here...

}

am thinking more like retrieve whole set from table DateTest, then in a loop (not sure which) I would go about checking if x = y these are the dates. Any code illustration would be highly appreciated. Regards,

  • 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-26T11:35:42+00:00Added an answer on May 26, 2026 at 11:35 am

    I haven’t looked at your code, but here are a few comments:

    • The range between a start date and end date is a TimeSpan, not a DateTime
    • If you are interested in reconstructing either the StartDate or EndDate, you can add or subtract the TimeSpan with the other value
    • If you are interested in checking for overlap, then you have a one-dimensional intersection test

    Here is how to do a 1-dimensional intersection test:

    One-Dimensional Line-Segments/Ranges Intersection Test: Solution Name?

    bool intersects = !(
        (activity1.End < activity2.Begin)
        || (activity2.End < activity1.Begin)
        );
    

    Once you have validated that there is an overlap between two date ranges, here is a way to calculate that overlap:

    DateTime end = activity1.End < activity2.End
        ? activity1.End
        : activity2.End;
    
    DateTime beginning = activity1.Begin > activity2.Begin
        ? activity1.Begin
        : activity2.Begin;
    

    With these checks in hand, I have a potential solution for you.

    Potential Solution

    You can make a special data structure that doesn’t contain individual appointments, but instead contains all consumed capacity.

    When you add a reservation to this data structure, you check if the new value overlaps existing reservations. You calculate its intersection between all existing values. For each value that it overlaps, add just the overlapped portion to a list, including the currently booked capacity at that overlap.

    Once all overlaps have been calculated, check if any of the items in the list have reached max capacity. If they have, then the booking is invalid, because it will make you over capacity. If they haven’t you can add the reservation.

    To add the reservation, increment all the intersection values’ booking counts by one. Then remove all existing values in the data structure on the range of the reservation. Split items if necessary to remove only the overlapped portion. When you are done, add the incremented intersections back into the list.

    You can do a similar but reversed operation when removing a booking, but you’ll have to merge bookings back together at the end/beginning of the range instead of splitting them apart.

    Or you can model this the opposite way, subtracting available capacity. Whichever makes more sense to you.

    This model will work well with modelling variable capacity because you can model your capacity with a similar data structure. You’d just do additional intersection tests between consumed capacity and available capacity, ensuring you always have enough available capacity to satisfy the booked capacity.

    I would be careful to design a system where this wasn’t the only data I relied on, and I could regenerate this data from data in a separate structure/table. For example, I would store the start and end dates and contact information for each booking separately.

    Optimization

    You can take advantage of sorting and space partitioning algorithms to speed up the part where you compare against all booked values. For example, you could partition out all the values into month-long chunks, so you only have to compare against the one or two months that your booking intersects. In relational database land this is called temporal partitioning.

    If you choose to do partitioning, one requirement that can make your life simpler is to make sure your partitions are large enough that you will only ever need to check two cells. Make your partitions smaller than this, and your algorithm will be more complicated and therefore harder to verify.

    If your data is small enough, you could calculate this whole table on the fly for just the date range you are interested in, rather than storing the data structure in the database. You might also not have to worry about partitioning if your ranges are small enough that you can stand to check every day (or whatever unit, if you have a different resolution) within the range. This would allow you to simplify some of the operations because you can just build up your data structure and do your capacity check, and you won’t have to worry about things like splitting up or merging capacity ranges.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it should be really simple to do but I just can't
This seems like it should be really simple, but I'm unable to figure this
This should be really simple, but I have tried many solutions posted on the
This should be really simple, but I can't seem to figure out what to
My problem seems to be really simple but I just can't get the reason
ok, this seems like it should be really simple but Im a bit confused:
This should be a really really simple thing, but for some reason it is
This should seem simple enough, but can't figure it out. I was porting a
Seems like this should be simple, but powershell is winning another battle with me.
This should be really simple. If I have a String like this: ../Test?/sample*.txt then

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.