Finding gap between Date ranges
-
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-Jan-2010 2-Jan-2010
-
1-Jan-2010 8-Jan-2010
-
4-Jan-2010 4-Jan-2010
-
6-Jan-2010 8-Jan-2010
-
9-Jan-2010 10-Jan-2010
-
10-Jan-2010 14-Jan-2010
-
10-Jan-2010 15-Jan-2010
-
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 followingGapStart 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
-
-
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-Jan-2010 2-Jan-2010
-
1-Jan-2010 8-Jan-2010
-
4-Jan-2010 4-Jan-2010
-
6-Jan-2010 8-Jan-2010
-
9-Jan-2010 10-Jan-2010
-
10-Jan-2010 14-Jan-2010
-
10-Jan-2010 15-Jan-2010
-
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 followingGapStart 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
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 setdates[currentDate]
to true (wherecurrentDate
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.
-
-
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 setdates[currentDate]
to true (wherecurrentDate
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.
-
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.
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)
andMax(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, wheredates[some date]
is false.Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
-
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)
andMax(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, wheredates[some date]
is false.Don't forget to rate answer, that helped you. It will allow other people find their answers faster.