c# - Getting Overlapping Start and End Date -
given daterange need return list of start , enddates overlaps given period.
what best way it? time in advance.
static void main(string[] args) { //period 1stmarch 20th april var startdate = new datetime(2011, 03, 1); var enddate = new datetime(2011, 4, 20); list<bookedperiod> bookedperiods=new list<bookedperiod>(); bookedperiods.add(new bookedperiod {startdate = new datetime(2011, 02, 5), enddate = new datetime(2011, 3, 15)}); bookedperiods.add(new bookedperiod { startdate = new datetime(2011, 03, 20), enddate = new datetime(2011, 4, 10) }); bookedperiods.add(new bookedperiod { startdate = new datetime(2011, 04, 01), enddate = new datetime(2011, 4, 15) }); list<overlappedperiod> myoverllappedperiods = getoverllapedperiods(startdate, enddate, bookedperiods); } public static list<overlappedperiod>getoverllapedperiods(datetime startdate,datetime enddate,list<bookedperiod>bookedperiods) { list<overlappedperiod>overlappedperiods=new list<overlappedperiod>(); // given daterange need return list of start , enddates overlaps //??how return overlappedperiods; } } public class bookedperiod { public datetime startdate { get; set; } public datetime enddate { get; set; } } public class overlappedperiod { public datetime startdate { get; set; } public datetime enddate { get; set; } }
edited
i have decided edit in hope of clarifing , therefore helping has me. confused overlapped vs intersect. scenario might help
i have booked subscription gym goes 01 march 11 20 april 11
i go on holiday between 05 feb 11 15 mar 11
i need start , end date when subscription valid not going gym result should startdate=1 march 2011 enddate =15 march 2011
myattempt: static void main() { //holiday period var startdate = new datetime(2011, 02, 5); var enddate = new datetime(2011, 3, 15);
list<bookedperiod> bookedperiods = new list<bookedperiod>(); bookedperiods.add(new bookedperiod { startdate = new datetime(2011, 02, 5), enddate = new datetime(2011, 4, 20) }); list<overlappedperiod> overlappedperiods=new list<overlappedperiod>(); foreach (var bookedperiod in bookedperiods) { datetime newstartdate = new datetime(); datetime newenddate = new datetime(); overlappedperiod overlappedperiod=new overlappedperiod(); overlappedperiod.startdate = newstartdate; overlappedperiod.enddate = newenddate; getdaterange(bookedperiod.startdate, bookedperiod.enddate, out newstartdate, out newenddate); overlappedperiods.add(overlappedperiod); } //do } private static void getdaterange(datetime startdate,datetime enddate,out datetime newstartdate,out datetime newenddate) { /* * need start , end date when subscription valid not going gym result should startdate=1 march 2011 enddate =15 march 2011 */ }
are looking dates within provided period, or partially?
completely within range:
var overlapped = period in bookedperiods period.startdate >= startdate && period.enddate <= enddate select new overlappedperiod { startdate = period.startdate, enddate = period.enddate }; overlappedperiods = overlapped.tolist();
partially overlapping:
var overlapped = period in bookedperiods (period.startdate >= startdate && period.enddate <= enddate) || (period.enddate >= startdate && period.enddate <= enddate) || (period.startdate <= startdate && period.enddate >= startdate) || (period.startdate <= startdate && period.enddate >= enddate) select new overlappedperiod { startdate = new datetime(math.max(period.startdate.ticks, startdate.ticks)), enddate = new datetime(math.min(period.enddate.ticks, enddate.ticks)) }; overlappedperiods = overlapped.tolist();
Comments
Post a Comment