0

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;
        }
     }
    }
   }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1032957
  • 453
  • 1
  • 6
  • 16
  • today belongs to a term or falls between two terms? – tariq Oct 24 '13 at 09:34
  • possible duplicate of [How to know if a DateTime is between a DateRange in C#](http://stackoverflow.com/questions/4781611/how-to-know-if-a-datetime-is-between-a-daterange-in-c-sharp) – Ian Oct 24 '13 at 09:35
  • if today falls between terms get the next terms first date – user1032957 Oct 24 '13 at 09:41
  • If my answer helped you, please accept it (green tick) :) – dav_i Oct 24 '13 at 10:08
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Oct 24 '13 at 10:09

3 Answers3

1

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);
dav_i
  • 27,509
  • 17
  • 104
  • 136
0
  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;
    }
user1032957
  • 453
  • 1
  • 6
  • 16
0
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;
}
tariq
  • 2,193
  • 15
  • 26