0

I got a list of DateTime which contains StartDate and EndDate, User Should Select one or more TimeSpans from this list. They are also have a structure class Named Courses.

How can I check if any overlaps is Happening or not

For example I got this

    Start Date   End Date
#1  7/20/2016    7/27/2016 Selected
#2  6/18/2016    7/25/2016 Selected
#3  7/20/2016    7/27/2016
#4  6/5/2016     6/10/2016

In this Example user has selected 2 dates that contains overlaps .

I want to warn the user with a message box or some Using C#.

Any opinion

Thanks

alireza amini
  • 1,712
  • 1
  • 18
  • 33
  • What do you mean by "that contains interferences"? What are "interferences" here? Your question isn't very clear at the moment. Do you mean you want to find which intervals overlap? It looks like the user hasn't selected *dates* but *date ranges* here... it looks to me like the first three all overlap each other... – Jon Skeet Jul 27 '16 at 05:59
  • I assume an interference means the Start Date and End Date of two selected courses overlap each other. – eds Jul 27 '16 at 06:01
  • 1
    Possible duplicate of [Determine Whether Two Date Ranges Overlap](http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap) – Aleksey L. Jul 27 '16 at 06:06
  • @AlekseyL. yeah thanks – alireza amini Jul 27 '16 at 06:07
  • @AlekseyL but can you write me a loop to do that , I got a Array And multiple Datetimes So... – alireza amini Jul 27 '16 at 06:09
  • You can simply test if the first date is greater than the second one – Chibueze Opata Jul 27 '16 at 06:11

4 Answers4

1

Ok, first created a class TimePeriod like this:

public class TimePeriod
{
    public int Id;
    public DateTime FromDate
    {
        get; set;
    }

    public DateTime ToDate
    {
        get; set;
    }

    public static DateTime Parse(string date)
    {
        var dt = DateTime.Parse(date,
        CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.RoundtripKind);
        return dt;
    }
}

Then created a List with items of this class:

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

Then added your examples of Dates (added all of them, for your need just add selected one's):

list.Add(new TimePeriod() { Id = 1, FromDate = TimePeriod.Parse("7/20/2016"), ToDate = TimePeriod.Parse("7/27/2016") });
list.Add(new TimePeriod() { Id = 2, FromDate = TimePeriod.Parse("6/18/2016"), ToDate = TimePeriod.Parse("7/25/2016") });
list.Add(new TimePeriod() { Id = 3, FromDate = TimePeriod.Parse("7/20/2016"), ToDate = TimePeriod.Parse("7/27/2016") });
list.Add(new TimePeriod() { Id = 4, FromDate = TimePeriod.Parse("6/5/2016"), ToDate = TimePeriod.Parse("6/10/2016") });

And last check with LINQ for overlapping:

var overlaps = from current in list
                from compare in list
                where
                (
                (compare.FromDate > current.FromDate &&
                compare.FromDate < current.ToDate) ||
                (compare.ToDate > current.FromDate &&
                compare.ToDate < current.ToDate)
                )
                select new
                {
                    Id1 = current.Id,
                    Id2 = compare.Id,
                };

The result will be in this case 1/2 & 2/1 and 2/3 & 3/2. In your case it will be 1/2 & 2/1.

c0d3b34n
  • 534
  • 7
  • 14
1

There is a very good library for working with time periods and their intersection on nuget.

Time Period Library

There is also an article on code project for it. Time Period Library for .NET

Sharkz
  • 458
  • 1
  • 9
  • 25
1

You need to store which dates have been selected and if they occur in multiple selections right?

Store startedate and enddate of each selected timespan to a Tuple selectedTimeSpans

then:

List<int> useddays =new List<int>();
foreach (Tuple<DateTime, DateTime> selected in selectedTimeSpans)
{
    DateTime start = selected.Value1;
    DateTime end = selected.Value2;
    DateTime current = start;
    while(current <=end)
    {
        if(useddays.Contains((current-DateTime.MinValue).TotalDays)
            MessageBox. Show("Already used!");
        else
             useddays.Add((current-DateTime.MinValue).TotalDays);
        current.AddDays(1);
    }
}
Thomas Voß
  • 1,145
  • 8
  • 20
0

Thanks To all @c0d3b34n and @ThomasVoß ,

Also to this article https://stackoverflow.com/a/325964/3970128

This is All I Have Done

Ok, first created a class TimePeriod like this:

public class TimePeriod { public int Id; public DateTime FromDate { get; set; }

public DateTime ToDate
{
    get; set;
}

public static DateTime Parse(string date)
{
    var dt = DateTime.Parse(date,
    CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.RoundtripKind);
    return dt;
}

} Then created a List with items of this class:

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

Then added your examples of Dates (added all of them, for your need just add selected one's):

list.Add(new TimePeriod() { Id = 1, FromDate = TimePeriod.Parse("7/20/2016"), ToDate = TimePeriod.Parse("7/27/2016") });
list.Add(new TimePeriod() { Id = 2, FromDate = TimePeriod.Parse("6/18/2016"), ToDate = TimePeriod.Parse("7/25/2016") });
list.Add(new TimePeriod() { Id = 3, FromDate = TimePeriod.Parse("7/20/2016"), ToDate = TimePeriod.Parse("7/27/2016") });
list.Add(new TimePeriod() { Id = 4, FromDate = TimePeriod.Parse("6/5/2016"), ToDate = TimePeriod.Parse("6/10/2016") });

Then

        foreach (var variable in list)
        {
            foreach (var VARIABLE in list)
            {
                if (variable.Id == VARIABLE.Id)
                {
                    continue;
                }
                if ((variable.FromDate <= VARIABLE.ToDate) && (variable.ToDate >= VARIABLE.FromDate))
                {
                    Console.WriteLine("Problem Hapendes!! {0} <= {1} , {2}  >= {3}", variable.FromDate.ToString(), VARIABLE.ToDate.ToString(), VARIABLE.ToDate.ToString(), VARIABLE.FromDate.ToString());
                }
            }
        }
Community
  • 1
  • 1
alireza amini
  • 1,712
  • 1
  • 18
  • 33