2013-11-03 54 views
1

我有管理事件到像綱領表web應用.. 對於每個事件我有一個的StartDate結束日期的範圍內。察看2個日期通過其它2個日期

在應用程序中,我想將事件導出到圖中,並且我想檢查事件是否通過了所需日期的範圍。

例如: 事件1:開始日期(9月9日)結束日期(9月14日)

我要檢查它是否能通過(9月10日至9月16日)

這是事件的照片示例圖形: enter image description here

這是檢查只有一個日期代碼:

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

編輯: 爲了澄清更多,我希望函數返回true,如果事件通過了2個日期範圍,即使它在範圍之前開始,或者在響應之後結束,它仍然應該返回true。

只有在不超過範圍時才返回false。

+0

可能DUP:http://stackoverflow.com/questions/3786821/check-如果日期範圍在日期範圍內 – Leon

回答

0

得到的回答是這樣的:

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)); 
    } 
0
public static bool Within(this DateTime current, DateTime startTime, DateTime endTime) 
    { 
     return startTime < currentTime && currentTime < endTime; 
    } 

,並調用它兩次

lowDate.Within(eventStart, eventEnd) && heDate.Within(eventStart, eventEnd) 
1
public static bool Within(DateTime one, DateTime two, 
          DateTime lowBound, DateTime highBound) 
{ 
    return one >= lowBound && two <= highBound; 
} 
+0

範圍內(新日期時間(2013,9,9),新日期時間(2013,9,14),新日期時間(2013,9,10),新的DateTime(2013,9,16)); 返回錯誤 –

+0

@AnasNaim沒錯,因爲09/09不在09/10(日/月)內。 – AgentFire