2012-04-25 92 views
-3

我有兩個由兩個DateTime值組成的類。開始日期和結束日期。檢查日期是否相交?

基本上,我需要一種方法來檢查一個類的開始日期 - 結束日期範圍是否與其他類相交。

是否有一個簡單的方法來做到這一點?

乾杯。

+5

'如果a.start b.start'然後A和B間隔相交 – zerkms 2012-04-25 02:12:07

+2

我不是故意要脫口而出,@Nick。你必須開玩笑問一個問題,第一天的編程學生會考慮N00Bis H。我不得不懷疑你正在拖延這個問題。 – JohnFx 2012-04-25 02:26:41

+0

@zerkms這不會檢測到所有的交叉點。如果a.start> b.start AND a.start b.end ... – Nick 2012-04-25 08:14:34

回答

4

下面是在其中時間可以重疊的可能方式(假設一個對象開始earlier和一個開始later)。

time ----------------------------------------------------------> 

(complete overlap) 
earlier :  start |------------------------------------| end 
later :   start |--------------------------| end 

(partial overlap) 
earlier :  start |--------------------------------| end 
later :   start |--------------------------------| end 

(start date matches) 
earlier :  start |----------------------------------| end 
later :  start |----------------------------------| end 

(no overlap) 
earlier :  start |------------| end 
later :        start |--------| end  

如果是這樣的話,你可以做以下檢查(雖然有

var earlier = obj1.start < obj2.start ? obj1 : obj2; 
var later = obj2.start < obj2.start ? obj1 : obj2; 

if (later.start < earlier.end) 
{ 
    Console.WriteLine("Object 1 overlaps with Object 2"); 
} 
+2

或者您可以使用我在評論中提出的單個比較:-) – zerkms 2012-04-25 06:53:48

2

您可以使用免費的Time Period Library for .NET

// ---------------------------------------------------------------------- 
public void TimeRangeSample() 
{ 
    // --- time range 1 --- 
    TimeRange timeRange1 = new TimeRange(
    new DateTime(2011, 2, 22, 14, 0, 0), 
    new DateTime(2011, 2, 22, 18, 0, 0)); 
    Console.WriteLine("TimeRange1: " + timeRange1); 
    // > TimeRange1: 22.02.2011 14:00:00 - 18:00:00 | 04:00:00 

    // --- time range 2 --- 
    TimeRange timeRange2 = new TimeRange(
    new DateTime(2011, 2, 22, 15, 0, 0), 
    new TimeSpan(2, 0, 0)); 
    Console.WriteLine("TimeRange2: " + timeRange2); 
    // > TimeRange2: 22.02.2011 15:00:00 - 17:00:00 | 02:00:00 

    // --- time range 3 --- 
    TimeRange timeRange3 = new TimeRange(
    new DateTime(2011, 2, 22, 16, 0, 0), 
    new DateTime(2011, 2, 22, 21, 0, 0)); 
    Console.WriteLine("TimeRange3: " + timeRange3); 
    // > TimeRange3: 22.02.2011 16:00:00 - 21:00:00 | 05:00:00 

    // --- relation --- 
    Console.WriteLine("TimeRange1.GetRelation(TimeRange2): " + 
        timeRange1.GetRelation(timeRange2)); 
    // > TimeRange1.GetRelation(TimeRange2): Enclosing 
    Console.WriteLine("TimeRange1.GetRelation(TimeRange3): " + 
        timeRange1.GetRelation(timeRange3)); 
    // > TimeRange1.GetRelation(TimeRange3): EndInside 
    Console.WriteLine("TimeRange3.GetRelation(TimeRange2): " + 
        timeRange3.GetRelation(timeRange2)); 
    // > TimeRange3.GetRelation(TimeRange2): StartInside 

    // --- intersection --- 
    Console.WriteLine("TimeRange1.GetIntersection(TimeRange2): " + 
        timeRange1.GetIntersection(timeRange2)); 
    // > TimeRange1.GetIntersection(TimeRange2): 
    //    22.02.2011 15:00:00 - 17:00:00 | 02:00:00 
    Console.WriteLine("TimeRange1.GetIntersection(TimeRange3): " + 
        timeRange1.GetIntersection(timeRange3)); 
    // > TimeRange1.GetIntersection(TimeRange3): 
    //    22.02.2011 16:00:00 - 18:00:00 | 02:00:00 
    Console.WriteLine("TimeRange3.GetIntersection(TimeRange2): " + 
        timeRange3.GetIntersection(timeRange2)); 
    // > TimeRange3.GetIntersection(TimeRange2): 
    //    22.02.2011 16:00:00 - 17:00:00 | 01:00:00 
} // TimeRangeSample