检查“重复日期”是否位于两个DateTime实例之间

时间:2014-03-11 08:24:31

标签: c# datetime

我们有一个名为ABDate的课程,其中包含Monthnullable YearDay。如果年份为ABDate,则创建null的实例时,日期将被视为重复出现。还有一个IsRecurring属性。

现在我们有一个IList<ABDate>,我们会得到一个开始和结束DateTime。我们如何检查列表中的ABDate是否位于开始和结束间隔中。

2 个答案:

答案 0 :(得分:2)

对于非经常性案例,解决方案是显而易见的。给定集合abDates以及startend个实例:

var anyInBetween = abDates
    .Select(ab => new DateTime(ab.Year.Value, ab.Month, ab.Day))
    .Any(d => d >= start && d <= end);

对于重复出现的情况,我们可以观察到,由于没有月份超过31天,因此f(month, day) = month * 100 + day等函数会将每个不同的(月,日)元组投影到一个唯一的整数中。比较两个这样的整数允许我们确定哪一个源元组在一年中的某一天代表:

// Note: m * 100 is "random". Multiplying by any factor > 31 will work as well.
Func<int, int, int> customTotalOrder = (m, d) => m * 100 + d;
var startIndex = customTotalOrder(start.Month, start.Day);
var endIndex = customTotalOrder(end.Month, end.Day);
var spanningYears = end.Year - start.Year;
var anyInBetween = false;

switch (spanningYears)
{
    default:
    case 2:
        // start => end contains at least one whole year, so obviously...
        anyInBetween = abDates.Any();
    case 1:
        var projection = abDates.Select(ab => customTotalOrder(ab.Month, ab.Day));
        anyInBetween = startIndex < endIndex
            ? projection.Any(i => i >= startIndex && i <= endIndex)
            : projection.Any(i => i >= startIndex || i <= endIndex);
    case 0:
        anyInBetween = abDates
            .Select(ab => customTotalOrder(ab.Month, ab.Day))
            .Any(i => i >= startIndex && i <= endIndex);
}

答案 1 :(得分:1)

 foreach (ABDate a in AbDateList)
  {

    for ( in checkYear = startDate.Year ;checkYear <= EndDate.Year; checkYear ++)
    {
         if ((new DateTime(checkYear ,a.Month,a.Day) >= startDate) && (new DateTime(checkYear ,a.Month,a.Day) <= endDate))
           {
              // I AM IN BETWEEN
           }
        }
    }
}