Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Finding gap between Date ranges

Finding gap between Date ranges

Scheduled Pinned Locked Moved C#
question
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DJ245
    wrote on last edited by
    #1

    Dear All, I have list of date ranges (Start Date and End Date). I want to find the gap between the each item. ie; My date range list

    #. Start Date End Date

    1.   1-Jan-2010	 2-Jan-2010
      
    2.   1-Jan-2010	 8-Jan-2010
      
    3.   4-Jan-2010	 4-Jan-2010
      
    4.   6-Jan-2010	 8-Jan-2010
      
    5.   9-Jan-2010	10-Jan-2010
      
    6.  10-Jan-2010	14-Jan-2010
      
    7.  10-Jan-2010	15-Jan-2010
      
    8.  17-Jan-2010	18-Jan-2010
      

    Actually i want to get the gap between adjacent items. Expected output

    GapStart GapEnd

    16-Jan-2010 16-Jan-2010 But i get the following

    GapStart GapEnd

    05-Jan-2010 05-Jan-2010 (Not need this gap because many other items include this date range) 16-Jan-2010 16-Jan-2010 How can i filter the resultant list?

    Regards, Lalk

    L 1 Reply Last reply
    0
    • D DJ245

      Dear All, I have list of date ranges (Start Date and End Date). I want to find the gap between the each item. ie; My date range list

      #. Start Date End Date

      1.   1-Jan-2010	 2-Jan-2010
        
      2.   1-Jan-2010	 8-Jan-2010
        
      3.   4-Jan-2010	 4-Jan-2010
        
      4.   6-Jan-2010	 8-Jan-2010
        
      5.   9-Jan-2010	10-Jan-2010
        
      6.  10-Jan-2010	14-Jan-2010
        
      7.  10-Jan-2010	15-Jan-2010
        
      8.  17-Jan-2010	18-Jan-2010
        

      Actually i want to get the gap between adjacent items. Expected output

      GapStart GapEnd

      16-Jan-2010 16-Jan-2010 But i get the following

      GapStart GapEnd

      05-Jan-2010 05-Jan-2010 (Not need this gap because many other items include this date range) 16-Jan-2010 16-Jan-2010 How can i filter the resultant list?

      Regards, Lalk

      L Offline
      L Offline
      Lukasz Nowakowski
      wrote on last edited by
      #2

      And how are you doing it right now? I would create a dictionary (Dictionary<DateTime, bool> dates) and fill it with all dates, that are available (in your example I would put there all dates between 1-Jan-2010 and 18-Jan-2010). Next there would be a loop over all ranges, and in this loop second loop over all dates in range. And I would set dates[currentDate] to true (where currentDate is date, that is pointed by double loop. After those loops you have a dictionary, where false is set on dates, that aren't in any of the ranges. Hope you get the idea.

      Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

      D 1 Reply Last reply
      0
      • L Lukasz Nowakowski

        And how are you doing it right now? I would create a dictionary (Dictionary<DateTime, bool> dates) and fill it with all dates, that are available (in your example I would put there all dates between 1-Jan-2010 and 18-Jan-2010). Next there would be a loop over all ranges, and in this loop second loop over all dates in range. And I would set dates[currentDate] to true (where currentDate is date, that is pointed by double loop. After those loops you have a dictionary, where false is set on dates, that aren't in any of the ranges. Hope you get the idea.

        Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

        D Offline
        D Offline
        DJ245
        wrote on last edited by
        #3

        If u don't mind, could u please post the code scrap here. I am using two loops and finding the difference between each date range (currentItem.StartDate - prevItem.EndDate). Then checking it with all the date renages.

        L 1 Reply Last reply
        0
        • D DJ245

          If u don't mind, could u please post the code scrap here. I am using two loops and finding the difference between each date range (currentItem.StartDate - prevItem.EndDate). Then checking it with all the date renages.

          L Offline
          L Offline
          Lukasz Nowakowski
          wrote on last edited by
          #4
          Dictionary<DateTime, bool> dates = new Dictionary<DateTime, bool>();
          for(DateTime loop = Min(ranges); loop < Max(ranges); loop = loop.AddDays(1))
          {
              dates.Add(loop, false);
          }
          
          for(int i = 0; i < ranges.count; i++)
          {
              for(DateTime loop = ranges[i].Start; loop < ranges[i].End; loop = loop.AddDays(1))
              {
                  dates[loop] = true;
              }
          }
          

          Min(ranges) and Max(ranges) give minimal and maximal range start (you must create them yourself). Rest of the code should compile and work, but I didn't check them. You will find gaps, where dates[some date] is false.

          Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

          D 1 Reply Last reply
          0
          • L Lukasz Nowakowski
            Dictionary<DateTime, bool> dates = new Dictionary<DateTime, bool>();
            for(DateTime loop = Min(ranges); loop < Max(ranges); loop = loop.AddDays(1))
            {
                dates.Add(loop, false);
            }
            
            for(int i = 0; i < ranges.count; i++)
            {
                for(DateTime loop = ranges[i].Start; loop < ranges[i].End; loop = loop.AddDays(1))
                {
                    dates[loop] = true;
                }
            }
            

            Min(ranges) and Max(ranges) give minimal and maximal range start (you must create them yourself). Rest of the code should compile and work, but I didn't check them. You will find gaps, where dates[some date] is false.

            Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

            D Offline
            D Offline
            DJ245
            wrote on last edited by
            #5

            Thanks Nowakowski. Its working fine..

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups