2011-05-06 109 views
0

我試圖比較一個特定的日期periodDate存儲在一個xml文件與一個stopDatestartDatec#比較日期時間和其他2日期

所以,如果periodDatestartDateStopDate之間插入數據:

if ((DateTime.Compare(periodTime, startDatePer1) == 1) 
    && (periodTime.CompareTo(stopDatePer1) == 1)) 
{ 
    // ... insert some data to databse     
} 
else if ((DateTime.Compare(periodTime, startDatePer2) == 1) 
     && (periodTime.CompareTo(stopDatePer2) == 1)) 
{ 
    // ..... insert data 
} 

我的問題是,即時通訊卡在第一if即使periodDate沒有startDatestopDate之間。

+0

'if(periodDate> = startDate && periodDate <= endDate)'是怎麼回事? – Bobby 2011-05-06 10:20:09

+0

你爲什麼卡住了,沒有讓你這麼做? – V4Vendetta 2011-05-06 10:23:56

回答

1

的問題是既DateTime.Compare(periodTime, startDatePer1) == 1periodTime.CompareTo(stopDatePer1) == 1轉化爲>

將第二個更改爲-1,它應該工作。但你並不需要使用CompareCompareTo,只是使用<>

4

爲什麼不:

if(periodDate > startDate && periodDate < stopDate) 
+0

感謝這工作perfekt – dumbel 2011-05-06 10:27:49

1
if (periodTime >= startDatePer1 && periodTime <= startDatePer2) 
{ 
    // Then insert data 
} 
0

最可讀的形式是:

if (startDate < periodTime && periodTime < stopDate) { 
} 

由於這更清楚地顯示了一段時間躺在之間的startDate和stopDate即的startDate < periodTime < stopDate

0

This庫包括類TimeRange支持vario我們的時間關係:

// ---------------------------------------------------------------------- 
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