I'm trying to get a certain number of date ranges given a start date. Here's my code:
var startDate = new DateTime(2014, 1, 1);
var dates = new List<DateBlock>();
int counter = 0;
while(counter < 6)
{
if (startDate >= DateTime.UtcNow)
{
dates.Add(new DateBlock
{
StartDate = startDate,
EndDate = startDate.AddDays(30)
});
counter++;
}
startDate = startDate.AddDays(30);
}
var first = dates.First();
dates.Insert(0, new DateBlock
{
StartDate = first.StartDate.AddDays(-30),
EndDate = first.StartDate
});
I'm wondering if there is a better way of doing this?
EDIT: If the start date is 1/1/1 it should loop through and increment by 30 days and only add to the list if the current date is greater than or equal to UtcNow. Also it should keep going into the future until 6 more DateBlock's are added to the list.