David Smith

David Smith

  • NA
  • 2k
  • 0

Creating a weekly event base upon a date range

Nov 28 2012 9:06 AM
Is there a better way to do this below.


I attempted below.

I want to come up with best way to design this logic. Creating a weekly service event base upon a daterange .

Weekly is every days from the start date. Within the range below there should be a total of 5 events.

ServiceStartDate = 11/01/2012 11:00PM

ServiceEndDate = 11/30/2012 11:00PM

public static IEnumerable<DateTime> GetDateRange(DateTime startDate, DateTime endDate)
{
if (endDate < startDate)
throw new ArgumentException("endDate must be greater than or equal to startDate");

while (startDate <= endDate)
{
yield return startDate;
startDate = startDate.AddDays(7);
}
} 



Answers (1)