Solved

check on overlapping dates

Posted on 2010-08-17
6
652 Views
Last Modified: 2013-11-27
I have an object which has two date values begin date and end date.  In fact there are multiple objects with begin date and end date. what i want to do is check that the dates of this objects dont overlap with each other. so for example object[0]'s begin date and end date is 08/16/2010 - 08/30/2010 and for object [1] begin date and end date is 08/20/2010-09/30/2010. if this the case then i wan to throw and error message.

can someone tell me how to do this in C#.

0
Comment
Question by:pratikshahse
6 Comments
 
LVL 27

Accepted Solution

by:
nmarun earned 250 total points
ID: 33457462
Here's a snippet that worked for me.Arun
public class Dummy

{

    public int Id { get; set; }

    public DateTime StartDate { get; set; }

    public DateTime EndDate { get; set; }

}



private static void CheckOverlapDates()

{

    List<Dummy> dummies = new List<Dummy>();

    dummies.Add(new Dummy

                    {

                        Id = 1,

                        StartDate = new DateTime(2010, 1, 1),

                        EndDate = new DateTime(2010, 1, 10)

                    });

    dummies.Add(new Dummy

                    {

                        Id = 2,

                        StartDate = new DateTime(2010, 1, 8),

                        EndDate = new DateTime(2010, 1, 20)

                    });

    dummies.Add(new Dummy

                    {

                        Id = 3,

                        StartDate = new DateTime(2010, 1, 20),

                        EndDate = new DateTime(2010, 1, 30)

                    });

    dummies.Add(new Dummy

                    {

                        Id = 4,

                        StartDate = new DateTime(2010, 1, 30),

                        EndDate = new DateTime(2010, 2, 10)

                    });

    dummies.Add(new Dummy

                    {

                        Id = 5,

                        StartDate = new DateTime(2010, 2, 10),

                        EndDate = new DateTime(2010, 2, 20)

                    });



    for (int i = 0; i < dummies.Count; i++)

    {

        for (int j = i+1; j < dummies.Count; j++)

        {

            if(dummies[i].EndDate > dummies[j].StartDate)

            {

                Console.WriteLine("object {0} overlaps with object {1}", dummies[i].Id, dummies[j].Id);

            }

        }

    }

}

Open in new window

0
 
LVL 16

Assisted Solution

by:kris_per
kris_per earned 250 total points
ID: 33457663
Logic is:
- if one's start date is within another's start/end dates
- or if one's end date is within another's start/end dates

public bool IsOverlapping()

        {

            List<MyObject> list = new List<MyObject>();



            foreach (MyObject o in list)

            {

                foreach (MyObject i in list)

                {

                    // logic is - if one start date is within another start/end dates

                    // or if one end date is within another start/end dates

                    if( (i.StartDate > o.StartDate && i.StartDate < o.EndDate)

                        || (i.EndDate > o.StartDate && i.EndDate < o.EndDate)

                        || (o.StartDate > i.StartDate && o.StartDate < i.EndDate)

                        || (o.EndDate > i.StartDate && o.EndDate < i.EndDate) )

                    {

                        return true; // yes overlapping

                    }

                }

            }



            return false; // not overlapping

        }

Open in new window

0
 
LVL 16

Expert Comment

by:kris_per
ID: 33457699

Contnuing previous code, If the overlapping is including the dates, then you have to add '=' operator in all comparisons above:
public static bool IsOverlapping()

        {

            List<MyObject> list = new List<MyObject>();



            foreach (MyObject o in list)

            {

                foreach (MyObject i in list)

                {

                    // logic is - if one start date is within another start/end dates

                    // or if one end date is within another start/end dates

                    if( (i.StartDate >= o.StartDate && i.StartDate <= o.EndDate)

                        || (i.EndDate >= o.StartDate && i.EndDate <= o.EndDate)

                        || (o.StartDate >= i.StartDate && o.StartDate <= i.EndDate)

                        || (o.EndDate >= i.StartDate && o.EndDate <= i.EndDate))

                    {

                        return true; // yes overlapping

                    }

                }

            }



            return false; // not overlapping

        }

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 2

Expert Comment

by:Sadrul
ID: 33458755
You should first sort the list in increasing start-date. After that, you can do a linear traversal of the list to figure out if two date-ranges overlap or not. This way, the worst-case running time would be nlog(n), whereas looking in the unsorted list will be n^2.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 33459291
About sorting (Sadrul's note) - just to remind that sorting also takes some time and has complexity, e.g., n*log(n)
0
 
LVL 2

Expert Comment

by:Sadrul
ID: 33459379
Correct. That is why I note that the overall complexity of the process will also be nlog(n) (sorting takes nlog(n), the linear traversal is n).
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 48
Not showing JavaScript in the list 5 40
How to add <appSettings> to the webconfig file 2 21
Way to decrease size of apk file 9 46
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

862 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now