Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

compare two dates

Posted on 2010-09-07
10
Medium Priority
?
632 Views
Last Modified: 2012-05-10
I have 4 date values :
 
first 2
begindate1 and enddate1

second 2
begindate2 and enddate2

i am going to compare begindate2 and enddate2 with begindate1 and enddate1 and the second set of dates cannot fall between first set of dates.

here is an example of what i want

begindate1 = 7/1/2010
enddate1 = 7/31/2010

if begindate2 = 6/1/2010
end date 2 = 6/30/2010 then true

if begindate2 = 8/1/2010
end date 2 = 8/30/2010 then true

if begindate2 = 7/10/2010
end date 2 = 7/20/2010 then false

how do i do this check.
0
Comment
Question by:pratikshahse
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 2

Expert Comment

by:gmilhon
ID: 33621862
Just check both values with one if statement...

if (begindate2 > begindate1) and (enddate2 < enddate1) {
    // True code here
} else {
    // False code here
}
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 33621947
I believe the following check would be sufficient:
        private void button1_Click(object sender, EventArgs e)
        {
            DateTime begindate1 = new DateTime(2010, 07, 01);
            DateTime enddate1 = new DateTime(2010, 07, 31);
            DateTime begindate2 = new DateTime(2010, 07, 10);
            DateTime enddate2 = new DateTime(2010, 07, 20);
            bool notOverlap = checkNotOverlapping(begindate1, enddate1, begindate2, enddate2);
            MessageBox.Show("result = " + notOverlap.ToString());
        }

        private bool checkNotOverlapping(DateTime begindate1, DateTime enddate1, DateTime begindate2, DateTime enddate2)
        {
            bool notOverlap = true;
            // argument checking:
            if((begindate1>enddate1) || (begindate2>enddate2))
            {
                throw new ArgumentException("Start date cannot be greater than end date");
            }

            if (!((begindate1 > enddate2) || begindate2 > enddate1))
            {
                notOverlap = false;
            }
            return notOverlap;
        }

Open in new window

0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 33621974
Really above I check if the second period starts AFTYER the first one , or vice versa. If we need to check the second one falls INTO the first one (or vice versa) - it will be a different check, something like like gmilhon showed.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:Mikkk
ID: 33622092

if begindate1 < begindate2 then
  if enddate1 < begindate2 then
    'not overlapped
  else
    'overlapped
  end if
else
  if enddate2 < begindate1 then
    'not overlapped
  else
    'overlapped
  end if
end if

Open in new window

0
 
LVL 14

Expert Comment

by:sentner
ID: 33622134
If you only care if the second date pair is fully encompased in the first range, and not just overlapping, then gmilhon's solution should work.

However, if you need to see if they overlap at all, you need something like:

if (begindate2 >= begindate1) and (begindate2 <= enddate1)
  or (enddate2 >= begindate1) and (enddate2 <= enddate1)
  or (begindate1 >= begindate2) and (begindate1 <= enddate2)
  or (enddate1 >= begindate2) and (enddate1 <= enddate2)  {
    // True code here
} else {
    // False code here
}
0
 
LVL 7

Accepted Solution

by:
illusio earned 2000 total points
ID: 33625164
geez what an if constructions...

if you are sure that the begindates are smaller then the enddates:
return  (edt2 < bdt1 || edt1 < bdt2);

if you aren't sure about that:
return (bdt1<edt1 && bdt2<edt2) && (edt2 < bdt1 || edt1 < bdt2);

Peter
0
 
LVL 2

Expert Comment

by:stagep
ID: 33630148
We need to know what you mean exactly by "the second set of dates cannot fall between first set of dates". Based on your example, what is the result for 6/15 to 7/15? true or false?
0
 

Author Comment

by:pratikshahse
ID: 33635524
stagep

the scenario that you are talking about in that case

if dates are (6/15 to 7/15)  (one of the dates falls in between the first date range)
{
message "overlapping dates found";
}
else
{
valid dates.
}
0
 

Author Comment

by:pratikshahse
ID: 33635544
stagep

if the dates are 7/15 - 8/15
{
message "overlapping dates found";
}
else
{
valid dates.

}
0
 
LVL 2

Expert Comment

by:stagep
ID: 33636271
then illusio has the correct answer as the second range of dates must either end before the first date range starts, or start after the the first date range ends.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question