当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - finding date between terms of dates

问题描述:

I have an array of terms which have start-dates and end-dates. how can I find whether today falls between the terms. code is

 var termsforcurrentCalendar = UnitOfWork.SchoolTerms.Query().Where(x

=> x.SchoolCalendarId == currentCalendar.SchoolCalendarId).ToArray();

var noOfTerms = termsforcurrentCalendar.Count();

for (int i = 0; i < noOfTerms; i++)

{

if (DateTime.Today > termsforcurrentCalendar[i].EndDate)

{

if (i != noOfTerms)

{

if (DateTime.Today < termsforcurrentCalendar[i + 1].StartDate)

{

datetoBeChecked = termsforcurrentCalendar[i + 1].StartDate;

}

}

}

}

网友答案:

How about using a bit of linq niceness:

var isTodayInTerm = termsforcurrentCalendar.Any(a => 
    DateTime.Today >= a.StartDate && DateTime.Today <= a.EndDate);

As per comment, to get the next term:

var nextTerm = termsforcurrentCalendar.OrderBy(a => a.StartDate).FirstOrDefault(a =>
    a.StartDate >= DateTime.Today);
网友答案:
  var isTodayInTerm = termsforcurrentCalendar.Any(a =>DateTime.Today >= a.StartDate && DateTime.Today <= a.EndDate);

    if (!isTodayInTerm)
    {
        var firstAvailableDate = termsforcurrentCalendar.FirstOrDefault(z => z.StartDate > DateTime.Today);
        datetoBeChecked = firstAvailableDate.StartDate;
    }
网友答案:
var isTodayInTerm = termsforcurrentCalendar.Any(a =>DateTime.Today >= a.StartDate && DateTime.Today <= a.EndDate);

if (!isTodayInTerm)
{
    var firstAvailableDate = termsforcurrentCalendar.OrderBy(a=>a.StartDate).FirstOrDefault(z => z.StartDate > DateTime.Today);
    datetoBeChecked = firstAvailableDate.StartDate;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: