How to avoid having dates which falls under a particular date range












1














My gridview accepts data for planning trips for a particular team. I need that their trips must not get clash with any other dates.



consider the following scenario.



------------------------------------------
| TripName | FromDate | ToDate |
------------------------------------------
| Delhi | 01/11/2018 | 05/11/2018 |
| Chennai | 07/11/2018 | 12/11/2018 |
| Bengaluru | 06/11/2018 | 11/11/2018 |
------------------------------------------


the last entry is invalid because it is clashing with the second trip. I need to avoid happening this.










share|improve this question




















  • 1




    What do you have so far? How do you add the trips to the GridView?
    – ikerbera
    Nov 20 at 6:58










  • i have got calenders in the gridview to accept dates, all i need is to check all the dates must not clash with each other before saving it to the database.
    – user1172910
    Nov 20 at 7:11










  • How do you define which is the good range? The first one in the grid takes priority?
    – ikerbera
    Nov 20 at 7:14










  • there are 2 calenders which are named as FromDate and Todate in 'n' number of rows of the gridview. so a date-range starts from FromDate and ends at ToDate. Now,The date-range selected in any row must not clash with any other date -range in any row.
    – user1172910
    Nov 20 at 7:18










  • post your code here, so everyone can check and fix it.
    – A_Name_Does_Not_Matter
    Nov 20 at 7:21
















1














My gridview accepts data for planning trips for a particular team. I need that their trips must not get clash with any other dates.



consider the following scenario.



------------------------------------------
| TripName | FromDate | ToDate |
------------------------------------------
| Delhi | 01/11/2018 | 05/11/2018 |
| Chennai | 07/11/2018 | 12/11/2018 |
| Bengaluru | 06/11/2018 | 11/11/2018 |
------------------------------------------


the last entry is invalid because it is clashing with the second trip. I need to avoid happening this.










share|improve this question




















  • 1




    What do you have so far? How do you add the trips to the GridView?
    – ikerbera
    Nov 20 at 6:58










  • i have got calenders in the gridview to accept dates, all i need is to check all the dates must not clash with each other before saving it to the database.
    – user1172910
    Nov 20 at 7:11










  • How do you define which is the good range? The first one in the grid takes priority?
    – ikerbera
    Nov 20 at 7:14










  • there are 2 calenders which are named as FromDate and Todate in 'n' number of rows of the gridview. so a date-range starts from FromDate and ends at ToDate. Now,The date-range selected in any row must not clash with any other date -range in any row.
    – user1172910
    Nov 20 at 7:18










  • post your code here, so everyone can check and fix it.
    – A_Name_Does_Not_Matter
    Nov 20 at 7:21














1












1








1







My gridview accepts data for planning trips for a particular team. I need that their trips must not get clash with any other dates.



consider the following scenario.



------------------------------------------
| TripName | FromDate | ToDate |
------------------------------------------
| Delhi | 01/11/2018 | 05/11/2018 |
| Chennai | 07/11/2018 | 12/11/2018 |
| Bengaluru | 06/11/2018 | 11/11/2018 |
------------------------------------------


the last entry is invalid because it is clashing with the second trip. I need to avoid happening this.










share|improve this question















My gridview accepts data for planning trips for a particular team. I need that their trips must not get clash with any other dates.



consider the following scenario.



------------------------------------------
| TripName | FromDate | ToDate |
------------------------------------------
| Delhi | 01/11/2018 | 05/11/2018 |
| Chennai | 07/11/2018 | 12/11/2018 |
| Bengaluru | 06/11/2018 | 11/11/2018 |
------------------------------------------


the last entry is invalid because it is clashing with the second trip. I need to avoid happening this.







c# asp.net gridview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 7:51









er-shoaib

4,9142516




4,9142516










asked Nov 20 at 6:56









user1172910

389




389








  • 1




    What do you have so far? How do you add the trips to the GridView?
    – ikerbera
    Nov 20 at 6:58










  • i have got calenders in the gridview to accept dates, all i need is to check all the dates must not clash with each other before saving it to the database.
    – user1172910
    Nov 20 at 7:11










  • How do you define which is the good range? The first one in the grid takes priority?
    – ikerbera
    Nov 20 at 7:14










  • there are 2 calenders which are named as FromDate and Todate in 'n' number of rows of the gridview. so a date-range starts from FromDate and ends at ToDate. Now,The date-range selected in any row must not clash with any other date -range in any row.
    – user1172910
    Nov 20 at 7:18










  • post your code here, so everyone can check and fix it.
    – A_Name_Does_Not_Matter
    Nov 20 at 7:21














  • 1




    What do you have so far? How do you add the trips to the GridView?
    – ikerbera
    Nov 20 at 6:58










  • i have got calenders in the gridview to accept dates, all i need is to check all the dates must not clash with each other before saving it to the database.
    – user1172910
    Nov 20 at 7:11










  • How do you define which is the good range? The first one in the grid takes priority?
    – ikerbera
    Nov 20 at 7:14










  • there are 2 calenders which are named as FromDate and Todate in 'n' number of rows of the gridview. so a date-range starts from FromDate and ends at ToDate. Now,The date-range selected in any row must not clash with any other date -range in any row.
    – user1172910
    Nov 20 at 7:18










  • post your code here, so everyone can check and fix it.
    – A_Name_Does_Not_Matter
    Nov 20 at 7:21








1




1




What do you have so far? How do you add the trips to the GridView?
– ikerbera
Nov 20 at 6:58




What do you have so far? How do you add the trips to the GridView?
– ikerbera
Nov 20 at 6:58












i have got calenders in the gridview to accept dates, all i need is to check all the dates must not clash with each other before saving it to the database.
– user1172910
Nov 20 at 7:11




i have got calenders in the gridview to accept dates, all i need is to check all the dates must not clash with each other before saving it to the database.
– user1172910
Nov 20 at 7:11












How do you define which is the good range? The first one in the grid takes priority?
– ikerbera
Nov 20 at 7:14




How do you define which is the good range? The first one in the grid takes priority?
– ikerbera
Nov 20 at 7:14












there are 2 calenders which are named as FromDate and Todate in 'n' number of rows of the gridview. so a date-range starts from FromDate and ends at ToDate. Now,The date-range selected in any row must not clash with any other date -range in any row.
– user1172910
Nov 20 at 7:18




there are 2 calenders which are named as FromDate and Todate in 'n' number of rows of the gridview. so a date-range starts from FromDate and ends at ToDate. Now,The date-range selected in any row must not clash with any other date -range in any row.
– user1172910
Nov 20 at 7:18












post your code here, so everyone can check and fix it.
– A_Name_Does_Not_Matter
Nov 20 at 7:21




post your code here, so everyone can check and fix it.
– A_Name_Does_Not_Matter
Nov 20 at 7:21












1 Answer
1






active

oldest

votes


















1














So if I understand well, you'd like to check if there is any intersect between any datetime.



Updated:



Define a class with intervals:



class DateTimeInterval
{
public DateTime Start { get; }
public DateTime End { get; }

public TimeSpan Length { get { return End - Start; } }
public DateTimeInterval(DateTime startDate, DateTime endDate)
{
if (endDate < startDate)
throw new ArgumentException();

Start = startDate;
End = endDate;
}

public bool Intersect(DateTimeInterval other)
{
if((other.Start <= Start && other.End >= Start) || //left intersect
(other.End >= End && other.Start <= End) || //right intersect
(other.Start >= Start && other.End <= End)) //middle intersect
{
return true;
}

return false;
}

static public bool Intersect(DateTimeInterval dti1, DateTimeInterval dti2)
{
if ((dti1.Start <= dti2.Start && dti1.End >= dti2.Start) || //left intersect
(dti1.End >= dti2.End && dti1.Start <= dti2.End) || //right intersect
(dti1.Start >= dti2.Start && dti1.End <= dti2.End)) //middle intersect
{
return true;
}

return false;
}
}


And use here a test example for the class:



List<DateTimeInterval> dtiList = new List<DateTimeInterval>();
DateTimeInterval dtInterval = new DateTimeInterval(new DateTime(2018, 09, 10), new DateTime(2018, 09, 20));

dtiList.Add(new DateTimeInterval(new DateTime(2018, 06, 12), new DateTime(2018, 7, 1)));
dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 05), new DateTime(2018, 09, 15)));
dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 12), new DateTime(2018, 09, 20)));
dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 11), new DateTime(2018, 09, 15)));
dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 5), new DateTime(2018, 09, 9)));

foreach(var dti in dtiList)
{
Console.WriteLine(DateTimeInterval.Intersect(dtInterval, dti));
}


If you have DateInterval0, DateInterval1, DateInterval2, DateInterval3, DateIntervalN you have to check intersects between




  • DateInterval0 and DateInterval1

  • DateInterval0 and DateInterval2

  • ...

  • DateInterval0 and DateIntervalN

  • ...

  • DateIntervalN-1 and DateIntervalN


And in your program, check intersect like:



for(var i = 0; i < dates.Length-2; ++i)
for(var j = i+1; i < dates.Length-1; ++j)
DateTimeInterval.Intersect(datesInterval[i], datesInterval[j]);





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387735%2fhow-to-avoid-having-dates-which-falls-under-a-particular-date-range%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    So if I understand well, you'd like to check if there is any intersect between any datetime.



    Updated:



    Define a class with intervals:



    class DateTimeInterval
    {
    public DateTime Start { get; }
    public DateTime End { get; }

    public TimeSpan Length { get { return End - Start; } }
    public DateTimeInterval(DateTime startDate, DateTime endDate)
    {
    if (endDate < startDate)
    throw new ArgumentException();

    Start = startDate;
    End = endDate;
    }

    public bool Intersect(DateTimeInterval other)
    {
    if((other.Start <= Start && other.End >= Start) || //left intersect
    (other.End >= End && other.Start <= End) || //right intersect
    (other.Start >= Start && other.End <= End)) //middle intersect
    {
    return true;
    }

    return false;
    }

    static public bool Intersect(DateTimeInterval dti1, DateTimeInterval dti2)
    {
    if ((dti1.Start <= dti2.Start && dti1.End >= dti2.Start) || //left intersect
    (dti1.End >= dti2.End && dti1.Start <= dti2.End) || //right intersect
    (dti1.Start >= dti2.Start && dti1.End <= dti2.End)) //middle intersect
    {
    return true;
    }

    return false;
    }
    }


    And use here a test example for the class:



    List<DateTimeInterval> dtiList = new List<DateTimeInterval>();
    DateTimeInterval dtInterval = new DateTimeInterval(new DateTime(2018, 09, 10), new DateTime(2018, 09, 20));

    dtiList.Add(new DateTimeInterval(new DateTime(2018, 06, 12), new DateTime(2018, 7, 1)));
    dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 05), new DateTime(2018, 09, 15)));
    dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 12), new DateTime(2018, 09, 20)));
    dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 11), new DateTime(2018, 09, 15)));
    dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 5), new DateTime(2018, 09, 9)));

    foreach(var dti in dtiList)
    {
    Console.WriteLine(DateTimeInterval.Intersect(dtInterval, dti));
    }


    If you have DateInterval0, DateInterval1, DateInterval2, DateInterval3, DateIntervalN you have to check intersects between




    • DateInterval0 and DateInterval1

    • DateInterval0 and DateInterval2

    • ...

    • DateInterval0 and DateIntervalN

    • ...

    • DateIntervalN-1 and DateIntervalN


    And in your program, check intersect like:



    for(var i = 0; i < dates.Length-2; ++i)
    for(var j = i+1; i < dates.Length-1; ++j)
    DateTimeInterval.Intersect(datesInterval[i], datesInterval[j]);





    share|improve this answer




























      1














      So if I understand well, you'd like to check if there is any intersect between any datetime.



      Updated:



      Define a class with intervals:



      class DateTimeInterval
      {
      public DateTime Start { get; }
      public DateTime End { get; }

      public TimeSpan Length { get { return End - Start; } }
      public DateTimeInterval(DateTime startDate, DateTime endDate)
      {
      if (endDate < startDate)
      throw new ArgumentException();

      Start = startDate;
      End = endDate;
      }

      public bool Intersect(DateTimeInterval other)
      {
      if((other.Start <= Start && other.End >= Start) || //left intersect
      (other.End >= End && other.Start <= End) || //right intersect
      (other.Start >= Start && other.End <= End)) //middle intersect
      {
      return true;
      }

      return false;
      }

      static public bool Intersect(DateTimeInterval dti1, DateTimeInterval dti2)
      {
      if ((dti1.Start <= dti2.Start && dti1.End >= dti2.Start) || //left intersect
      (dti1.End >= dti2.End && dti1.Start <= dti2.End) || //right intersect
      (dti1.Start >= dti2.Start && dti1.End <= dti2.End)) //middle intersect
      {
      return true;
      }

      return false;
      }
      }


      And use here a test example for the class:



      List<DateTimeInterval> dtiList = new List<DateTimeInterval>();
      DateTimeInterval dtInterval = new DateTimeInterval(new DateTime(2018, 09, 10), new DateTime(2018, 09, 20));

      dtiList.Add(new DateTimeInterval(new DateTime(2018, 06, 12), new DateTime(2018, 7, 1)));
      dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 05), new DateTime(2018, 09, 15)));
      dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 12), new DateTime(2018, 09, 20)));
      dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 11), new DateTime(2018, 09, 15)));
      dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 5), new DateTime(2018, 09, 9)));

      foreach(var dti in dtiList)
      {
      Console.WriteLine(DateTimeInterval.Intersect(dtInterval, dti));
      }


      If you have DateInterval0, DateInterval1, DateInterval2, DateInterval3, DateIntervalN you have to check intersects between




      • DateInterval0 and DateInterval1

      • DateInterval0 and DateInterval2

      • ...

      • DateInterval0 and DateIntervalN

      • ...

      • DateIntervalN-1 and DateIntervalN


      And in your program, check intersect like:



      for(var i = 0; i < dates.Length-2; ++i)
      for(var j = i+1; i < dates.Length-1; ++j)
      DateTimeInterval.Intersect(datesInterval[i], datesInterval[j]);





      share|improve this answer


























        1












        1








        1






        So if I understand well, you'd like to check if there is any intersect between any datetime.



        Updated:



        Define a class with intervals:



        class DateTimeInterval
        {
        public DateTime Start { get; }
        public DateTime End { get; }

        public TimeSpan Length { get { return End - Start; } }
        public DateTimeInterval(DateTime startDate, DateTime endDate)
        {
        if (endDate < startDate)
        throw new ArgumentException();

        Start = startDate;
        End = endDate;
        }

        public bool Intersect(DateTimeInterval other)
        {
        if((other.Start <= Start && other.End >= Start) || //left intersect
        (other.End >= End && other.Start <= End) || //right intersect
        (other.Start >= Start && other.End <= End)) //middle intersect
        {
        return true;
        }

        return false;
        }

        static public bool Intersect(DateTimeInterval dti1, DateTimeInterval dti2)
        {
        if ((dti1.Start <= dti2.Start && dti1.End >= dti2.Start) || //left intersect
        (dti1.End >= dti2.End && dti1.Start <= dti2.End) || //right intersect
        (dti1.Start >= dti2.Start && dti1.End <= dti2.End)) //middle intersect
        {
        return true;
        }

        return false;
        }
        }


        And use here a test example for the class:



        List<DateTimeInterval> dtiList = new List<DateTimeInterval>();
        DateTimeInterval dtInterval = new DateTimeInterval(new DateTime(2018, 09, 10), new DateTime(2018, 09, 20));

        dtiList.Add(new DateTimeInterval(new DateTime(2018, 06, 12), new DateTime(2018, 7, 1)));
        dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 05), new DateTime(2018, 09, 15)));
        dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 12), new DateTime(2018, 09, 20)));
        dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 11), new DateTime(2018, 09, 15)));
        dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 5), new DateTime(2018, 09, 9)));

        foreach(var dti in dtiList)
        {
        Console.WriteLine(DateTimeInterval.Intersect(dtInterval, dti));
        }


        If you have DateInterval0, DateInterval1, DateInterval2, DateInterval3, DateIntervalN you have to check intersects between




        • DateInterval0 and DateInterval1

        • DateInterval0 and DateInterval2

        • ...

        • DateInterval0 and DateIntervalN

        • ...

        • DateIntervalN-1 and DateIntervalN


        And in your program, check intersect like:



        for(var i = 0; i < dates.Length-2; ++i)
        for(var j = i+1; i < dates.Length-1; ++j)
        DateTimeInterval.Intersect(datesInterval[i], datesInterval[j]);





        share|improve this answer














        So if I understand well, you'd like to check if there is any intersect between any datetime.



        Updated:



        Define a class with intervals:



        class DateTimeInterval
        {
        public DateTime Start { get; }
        public DateTime End { get; }

        public TimeSpan Length { get { return End - Start; } }
        public DateTimeInterval(DateTime startDate, DateTime endDate)
        {
        if (endDate < startDate)
        throw new ArgumentException();

        Start = startDate;
        End = endDate;
        }

        public bool Intersect(DateTimeInterval other)
        {
        if((other.Start <= Start && other.End >= Start) || //left intersect
        (other.End >= End && other.Start <= End) || //right intersect
        (other.Start >= Start && other.End <= End)) //middle intersect
        {
        return true;
        }

        return false;
        }

        static public bool Intersect(DateTimeInterval dti1, DateTimeInterval dti2)
        {
        if ((dti1.Start <= dti2.Start && dti1.End >= dti2.Start) || //left intersect
        (dti1.End >= dti2.End && dti1.Start <= dti2.End) || //right intersect
        (dti1.Start >= dti2.Start && dti1.End <= dti2.End)) //middle intersect
        {
        return true;
        }

        return false;
        }
        }


        And use here a test example for the class:



        List<DateTimeInterval> dtiList = new List<DateTimeInterval>();
        DateTimeInterval dtInterval = new DateTimeInterval(new DateTime(2018, 09, 10), new DateTime(2018, 09, 20));

        dtiList.Add(new DateTimeInterval(new DateTime(2018, 06, 12), new DateTime(2018, 7, 1)));
        dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 05), new DateTime(2018, 09, 15)));
        dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 12), new DateTime(2018, 09, 20)));
        dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 11), new DateTime(2018, 09, 15)));
        dtiList.Add(new DateTimeInterval(new DateTime(2018, 09, 5), new DateTime(2018, 09, 9)));

        foreach(var dti in dtiList)
        {
        Console.WriteLine(DateTimeInterval.Intersect(dtInterval, dti));
        }


        If you have DateInterval0, DateInterval1, DateInterval2, DateInterval3, DateIntervalN you have to check intersects between




        • DateInterval0 and DateInterval1

        • DateInterval0 and DateInterval2

        • ...

        • DateInterval0 and DateIntervalN

        • ...

        • DateIntervalN-1 and DateIntervalN


        And in your program, check intersect like:



        for(var i = 0; i < dates.Length-2; ++i)
        for(var j = i+1; i < dates.Length-1; ++j)
        DateTimeInterval.Intersect(datesInterval[i], datesInterval[j]);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 at 8:17

























        answered Nov 20 at 7:27









        koviroli

        454311




        454311






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387735%2fhow-to-avoid-having-dates-which-falls-under-a-particular-date-range%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?