Sounds like a homework? no it's not. I worked up the logic for this but not such a performant one when dates are span over years. Basically here is how it should work,
StartDate: 1/1/2012
FinishDate: 1/10/2012
RecurringInterval: 2 ( In days)
Output would be:
1/6/2012
if Todays date (Date.Now
) is 1/5/2012
( Assuming format MM/dd/yyyy
). Check would end when finish date is reached. If no dates match within given time period, today's Date must be returned. Dead simple but not a efficient one.
What is wrong with this?
if (!_isRecurring)
return DateTime.UtcNow;
DateTime initialDate = _startDate;
DateTime finalDate = _finishDate;
int recurringDays = _recurringInteral;
/*
* start Date + recurring interval falls between start date and finishdate then get its date
*/
do
{
//add recurring day to start date
initialDate = initialDate.AddDays(recurringDays);
//check if it falls in between start days and end days
if(initialDate <= finalDate)
break;
} while (initialDate <= finalDate);
//return the first occurance of the recurring day
return initialDate;