public static List <DateTime> GeneratePeriods(GeneratePeriodRequest request) { var result = new List <DateTime>() { request.StartDate }; do { var nextPeriod = request.StartDate .AddMonths(request.MonthsPerPeriod); result.Add(nextPeriod); request.StartDate = nextPeriod; }while (request.StartDate < request.EndDate); return(result); }
static void Main(string[] args) { var stopWatch = new Stopwatch(); stopWatch.Start(); System.Console.WriteLine("Periods:"); var startDate = new DateTime(2000, 2, 1); var endDate = DateTime.Now; var monthsPerPeriod = 1; var request = new GeneratePeriodRequest { StartDate = startDate, EndDate = endDate, MonthsPerPeriod = monthsPerPeriod }; foreach (var period in GeneratePeriods(request)) { Console.WriteLine(period); } var getPeriodResult = GetPeriod(new GetPeriodRequest { StartDate = startDate, CurrentDate = endDate, MonthsPerPeriod = monthsPerPeriod }); System.Console.WriteLine("Current periods:"); foreach (var period in getPeriodResult.CurrentPeriod.Months) { System.Console.WriteLine(period); } System.Console.WriteLine("Previous periods:"); foreach (var period in getPeriodResult.PreviousPeriod.Months) { System.Console.WriteLine(period); } stopWatch.Stop(); Console.WriteLine(stopWatch.ElapsedMilliseconds); }