1

I have a WebApplication that manages events into tables like an Agenda.. For each event I have a StartDate and EndDate.

In the Application I want to export the events into a graph, and I want to check if the event passes the range of the required dates.

For example: Event 1: StartDate (9 September) EndDate (14 September)

I want to check if it passes (10 Sept - 16 Sept)

This is a photo of the events example Graph: enter image description here

This is a code that checks for only one date:

public static bool Within(this DateTime current, DateTime startTime, DateTime endTime)
    {
        return startTime < currentTime < endTime;
    }

Edit: To clarify more, I want the function to return true if the event passes the 2 dates range, even if it start before the range or ends after the rang it should return true anyway.

only return false if it does not pass the range.

Anas Naim
  • 313
  • 2
  • 4
  • 14
  • possible dup: http://stackoverflow.com/questions/3786821/check-if-a-date-range-is-within-a-date-range – Leon Nov 03 '13 at 16:14

3 Answers3

1
public static bool Within(DateTime one, DateTime two,
                          DateTime lowBound, DateTime highBound)
{
    return one >= lowBound && two <= highBound;
}
AgentFire
  • 8,944
  • 8
  • 43
  • 90
0
public static bool Within(this DateTime current, DateTime startTime, DateTime endTime)
    {
        return startTime < currentTime && currentTime < endTime;
    }

And call it twice

lowDate.Within(eventStart, eventEnd) && heDate.Within(eventStart, eventEnd)
sh1ng
  • 2,808
  • 4
  • 24
  • 38
0

Got the answer this way:

public static bool Within(DateTime StartDate, DateTime EndDate, DateTime StartRange, DateTime EndRange)
    {
        if ((StartDate == EndDate) && (StartRange <= StartDate && StartDate <= EndRange))
            return true;
        else
            return ((StartRange >= StartDate && StartRange <= EndDate) || (EndRange >= StartDate && EndRange <= EndDate));
    }
Anas Naim
  • 313
  • 2
  • 4
  • 14