HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Date ranges from a given start date

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
daterangesstartfromgiven

Problem

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();
int counter = 0;
while(counter = 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.

Solution

The loop to find the starting date is clearly not needed. This can be done with library object TimeSpan -- like this:

void Main()
{
  var startDate = new DateTime(2014, 1, 1);
  var dates = new List();

  int priorPeriods = ((DateTime.UtcNow-startDate).Days) / 30;

  DateTime dateIndex = startDate.AddDays(priorPeriods*30);
  for(int index = 0; index < 6; index++)
  {
     dates.Add(new DateBlock { StartDate = dateIndex, EndDate = dateIndex.AddDays(30) });
     dateIndex = dateIndex.AddDays(30);
  }
}

public class DateBlock
{
   public DateTime StartDate { get; set;}
   public DateTime EndDate { get; set; }
}


You might have to check the edge case (today is exactly divisible by 30) to see it matches requirements.

Here is how you do it with linq (looks cooler, but slower)

void Main()
{
  var startDate = new DateTime(2014, 1, 1);
  var dates = new List();

  int priorPeriods = ((DateTime.UtcNow-startDate).Days) / 30;

  dates = Enumerable.Range(0,6)
                .Select(index => new DateBlock {
                                   StartDate = startDate.AddDays((priorPeriods+index)*30),
                                   EndDate =startDate.AddDays((priorPeriods+index+1)*30)
                }).ToList();
 }

public class DateBlock
{
   public DateTime StartDate { get; set;}
   public DateTime EndDate { get; set; }
}

Code Snippets

void Main()
{
  var startDate = new DateTime(2014, 1, 1);
  var dates = new List<DateBlock>();

  int priorPeriods = ((DateTime.UtcNow-startDate).Days) / 30;

  DateTime dateIndex = startDate.AddDays(priorPeriods*30);
  for(int index = 0; index < 6; index++)
  {
     dates.Add(new DateBlock { StartDate = dateIndex, EndDate = dateIndex.AddDays(30) });
     dateIndex = dateIndex.AddDays(30);
  }
}

public class DateBlock
{
   public DateTime StartDate { get; set;}
   public DateTime EndDate { get; set; }
}
void Main()
{
  var startDate = new DateTime(2014, 1, 1);
  var dates = new List<DateBlock>();

  int priorPeriods = ((DateTime.UtcNow-startDate).Days) / 30;

  dates = Enumerable.Range(0,6)
                .Select(index => new DateBlock {
                                   StartDate = startDate.AddDays((priorPeriods+index)*30),
                                   EndDate =startDate.AddDays((priorPeriods+index+1)*30)
                }).ToList();
 }

public class DateBlock
{
   public DateTime StartDate { get; set;}
   public DateTime EndDate { get; set; }
}

Context

StackExchange Code Review Q#46343, answer score: 4

Revisions (0)

No revisions yet.