I need to calculate some Date Ranges.
I have three Date Ranges:
- 25 July 11 - 10 Oct 11
- 05 Aug 11 - 05 Oct 11
20 Aug 11 - 08 Nov 11
I need to create a new membership for all the valid periods that can be before and after
What is a good way to do this?
Code Example
public enum MembershipType { Gym=1, Magazine=2, Hotel=3 } class Program { static void Main() { // During 1st Aug 2011 and 20th Oct 2011 Membership payment must be stopped var stopPaymentStartDate = new DateTime(2011,08,1); var stopPaymentEndDate = new DateTime(2011,10,20); var gymMembership = new Membership{MembershipType=MembershipType.Gym, StartDate = new DateTime(2011, 07, 25), EndDate = new DateTime(2011, 10, 10)}; var magazineMembership = new Membership{MembershipType = MembershipType.Magazine,StartDate = new DateTime(2011, 08, 5),EndDate = new DateTime(2011, 10, 5)}; var hotelMembership = new Membership{MembershipType = MembershipType.Hotel,StartDate = new DateTime(2011, 08, 20),EndDate = new DateTime(2011, 11, 8)}; //How Can I get the wanted result below: //??? //Wanted List of result as follows: var gymMembershipResult = new Result{MembershipType = MembershipType.Gym,StartDate = new DateTime(2011, 10, 10),EndDate = new DateTime(2011, 10, 20)}; var magazineMembershipResultOne = new Result { MembershipType = MembershipType.Magazine, StartDate = new DateTime(2011, 08, 01), EndDate = new DateTime(2011, 08,05) }; var magazineMembershipResultTwo = new Result { MembershipType = MembershipType.Magazine, StartDate = new DateTime(2011, 10, 05), EndDate = new DateTime(2011, 10, 20) }; var hotelMembershipResult = new Result { MembershipType = MembershipType.Hotel, StartDate = new DateTime(2011, 08, 01), EndDate = new DateTime(2011, 08, 20) }; } } public class Result { public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public MembershipType MembershipType { get; set; } } public class Membership { public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public MembershipType MembershipType { get; set; } } } 开发者_StackOverflow社区
class Program
{
static void Main(string[] args)
{
DateTime stopPaymentStartDate = new DateTime(2011, 08, 1);
DateTime stopPaymentEndDate = new DateTime(2011, 10, 20);
MembershipPeriod gymMembership = new MembershipPeriod
{
MembershipType = MembershipType.Gym,
StartDate = new DateTime(2011, 07, 25),
EndDate = new DateTime(2011, 10, 10)
};
MembershipPeriod magazineMembership = new MembershipPeriod
{
MembershipType = MembershipType.Magazine,
StartDate = new DateTime(2011, 08, 5),
EndDate = new DateTime(2011, 10, 5)
};
MembershipPeriod hotelMembership = new MembershipPeriod
{
MembershipType = MembershipType.Hotel,
StartDate = new DateTime(2011, 08, 20),
EndDate = new DateTime(2011, 11, 8)
};
List<MembershipPeriod> resultsGym = GetResultsForPeriod(gymMembership, stopPaymentStartDate, stopPaymentEndDate);
List<MembershipPeriod> resultsmagazine = GetResultsForPeriod(magazineMembership, stopPaymentStartDate, stopPaymentEndDate);
List<MembershipPeriod> resultshotel = GetResultsForPeriod(hotelMembership, stopPaymentStartDate, stopPaymentEndDate);
}
private static List<MembershipPeriod> GetResultsForPeriod(MembershipPeriod period, DateTime stopPaymentStartDate, DateTime stopPaymentEndDate)
{
List<DateTime> datesToKeep = new List<DateTime>();
for (DateTime date = stopPaymentStartDate.Date; date <= stopPaymentEndDate.Date; date = date.AddDays(1))
{
if (date <= period.StartDate.Date || date >= period.EndDate.Date)
{
datesToKeep.Add(date);
}
}
List<MembershipPeriod> results = new List<MembershipPeriod>();
MembershipPeriod newPeriod = null;
for (int i = 0; i < datesToKeep.Count; i++)
{
if (newPeriod == null)
{
newPeriod = new MembershipPeriod();
newPeriod.MembershipType = period.MembershipType;
newPeriod.StartDate = datesToKeep[i];
}
if (i == datesToKeep.Count - 1 || (datesToKeep[i + 1] - datesToKeep[i]).Days > 1)
{
newPeriod.EndDate = datesToKeep[i];
results.Add(newPeriod);
newPeriod = null;
}
}
return results;
}
}
public enum MembershipType
{
Gym = 1,
Magazine = 2,
Hotel = 3
}
public class MembershipPeriod
{
public DateTime StartDate
{
get;
set;
}
public DateTime EndDate
{
get;
set;
}
public MembershipType MembershipType
{
get;
set;
}
}
精彩评论