我需要计算一些日期范围。
我有三个日期范围:
8月20日 - 11月8日11月
我需要为
之前和之后的所有有效期创建新的成员资格这样做的好方法是什么?
代码示例
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; }
}
}
答案 0 :(得分:1)
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;
}
}