c# - Date Range Calculation .Any Suggestions? -
i need calculate date ranges.
i have 3 date ranges:
- 25 july 11 - 10 oct 11
- 05 aug 11 - 05 oct 11
20 aug 11 - 08 nov 11
i need create new membership valid periods can before , after
what way this?
code example
public enum membershiptype { gym=1, magazine=2, hotel=3 } class program { static void main() { // during 1st aug 2011 , 20th oct 2011 membership payment must 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 wanted result below: //??? //wanted list of result 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; } } }
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>(); (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; (int = 0; < 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; } }
Comments
Post a Comment