Reputation: 16040
Given a DateRange I need to return a list of Start and EndDates that overlaps the given period.
what is the best way to do it? Thanks for your time in advance.
static void Main(string[] args)
{
//Period 1stMarch to 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 a DateRange I need to return a list of Start and EndDates that overlaps
//??how I do i
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; }
}
I have decided to edit in the hope of clarifing and therefore helping who has to help me. I get confused by overlapped vs intersect. A scenario might help
I have booked a subscription to the gym that goes from 01 March 11 to 20 April 11
I go on holiday between 05 Feb 11 to 15 Mar 11
I need to get the start and end date when my subscription is valid that I will NOT BE GOING TO THE GYM The result I should get back is 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 something with it
}
private static void GetDateRange(DateTime startDate,DateTime endDate,out DateTime newStartDate,out DateTime newEndDate)
{
/*
* I need to get the start and end date when my subscription is valid that I will NOT BE GOING TO THE GYM
The result I should get back is StartDate=1 March 2011 EndDate =15 March 2011
*/
}
Upvotes: 0
Views: 3558
Reputation: 35890
Are you looking for dates that are completely within the provided period, or only partially?
Completely within the range:
var overlapped =
from period in bookedPeriods
where period.StartDate >= startDate && period.EndDate <= endDate
select new OverlappedPeriod { StartDate = period.StartDate, EndDate = period.EndDate };
overlappedPeriods = overlapped.ToList();
Partially overlapping:
var overlapped =
from period in bookedPeriods
where (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();
Upvotes: 2
Reputation: 25799
One method would be to make use of the Rectangle class to calculate intersects. So the procedure would be to create a rectangle for each date range then use the Rectangle.Intersect( ) method.
Upvotes: 1
Reputation: 16603
this piece of LINQ might help:
var overlappedPeriods = bookedPeriods.Where(p=>p.EndDate > startDate && p.StartDate < endDate);
then transform the results accordingly to your OverlappedPeriod class
Upvotes: 2
Reputation: 8421
Something like this (not tested sorry):
return bookedPeriods.Where(
b => (b.StartDate > startDate && b.StartDate < endDate) ||
(b.EndDate> startDate && b.EndDate < endDate) ||
(b.StartDate < startDate && b.EndDate > endDate)
).ToList()
Upvotes: 1