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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:45:54+00:00 2026-05-25T11:45:54+00:00

I have four DateTime objects. A1, A2 and B1, B2. I need to know

  • 0

I have four DateTime objects.
A1, A2 and B1, B2.

I need to know that the period A1-A2 doesn’t intersect with period B1-B2. But I don`t want to write dirty code, like many if blocks.

if (A1 < B1 && A2 > B1)
{
    return false;
}

….
etc.

EDITED

I tried to use this one: Comparing ranges

DateTime A1 = DateTime.MinValue.AddMinutes(61);
DateTime A2 = DateTime.MinValue.AddHours(1.2);
DateTime B1 = DateTime.MinValue.AddMinutes(5);
DateTime B2 = DateTime.MinValue.AddHours(1);

Console.WriteLine(Range.Overlap(
    new Range<DateTime>(A1, A2),
    new Range<DateTime>(B1, B2)
));

It returned true but I expected false.
Because this code always returns true

 if (left.Start.CompareTo(left.Start) == 0)
 {
     return true;
 }
  • 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-25T11:45:54+00:00Added an answer on May 25, 2026 at 11:45 am

    I dont believe there is going to be any manner of ‘easy’ code to write; you have to account for 4 distinct use cases. If you need to do this kind of check a lot, I’d write an extension method. Otherwise, you just need to check these conditions:

     |--- Date 1 ---|
          | --- Date 2 --- |
    
    
          | --- Date 1 --- |
     | --- Date 2 ---- |
    
    
     | -------- Date 1 -------- |
          | --- Date 2 --- |
    
          | --- Date 1 --- |
     | -------- Date 2 -------- |
    

    EDIT: To provide actual code:

    public class DateTimeRange
    {
         public DateTime Start { get; set; }
         public DateTime End { get; set; }
    
         public bool Intersects(DateTimeRange test)
         {
             if(this.Start > this.End || test.Start > test.End)
                throw new InvalidDateRangeException();
    
             if(this.Start == this.End || test.Start == test.End)
                  return false; // No actual date range
    
             if(this.Start == test.Start || this.End == test.End)
                  return true; // If any set is the same time, then by default there must be some overlap. 
    
             if(this.Start < test.Start)
             {
                  if(this.End > test.Start && this.End < test.End)
                      return true; // Condition 1
    
                  if(this.End > test.End)
                      return true; // Condition 3
             }
             else
             {
                  if(test.End > this.Start && test.End < this.End)
                      return true; // Condition 2
    
                  if(test.End > this.End)
                      return true; // Condition 4
             }
    
             return false;
        }
    }
    

    That should cover the use cases.

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

Sidebar

Related Questions

I have four tables containing exactly the same columns, and want to create a
I have four models that are related to one another, the way I have
I have some databases that have four files each; one for PRIMARY, IDX, IMAGE,
I have four textfields that bind to the model key path. If a number
I have created the SQL script below that creates four tables and inserts data
I have four foreach loops that iterate through the collections and based on a
I have four points: a=(-0.5, -0.5) b=(-0.5, 0.5) c=(0.3, -0.5) d=(0.0, 1.0) I need
I have four divs that are draggable. And a div where I drop the
I have an entity BattleUser which contains four properties: Id, BattleId, UserId and DateTime.
I have four models: a, b, c, d here is what I want to

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.