听起来像家庭作业?不,这不对。我为此制定了逻辑,但当日期跨越数年时,我就没有那么高效了。基本上这是它应该如何工作,
StartDate: 1/1/2012
FinishDate: 1/10/2012
RecurringInterval: 2 ( In days)
输出将是:
1/6/2012
如果今天的日期(Date.Now
)是1/5/2012
(假设格式MM/dd/yyyy
)。到达完成日期时,检查将结束。如果在给定的时间段内没有匹配的日期,则必须返回今天的日期。死的简单但不是一个有效的。
这有什么问题?
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;