DateTime.CompareTo 方法

定義

比較這個執行個體的值與指定的 DateTime 值,指出這個執行個體早於、同於或晚於指定的 DateTime 值。

多載

CompareTo(DateTime)

比較這個執行個體的值與指定的 DateTime 值,並且傳回一個整數,指出這個執行個體是早於、同於或晚於指定的 DateTime 值。

CompareTo(Object)

比較這個執行個體的值與含有指定之 DateTime 值的指定物件,並且傳回一個整數,指出這個執行個體是早於、同於或晚於指定的 DateTime 值。

備註

方法的兩個 CompareTo 多載會傳回帶正負號的數位,指出這個實例的相對值和 value 引數,如下表所示。

描述
小於零 這個執行個體早於 value
這個執行個體和 value 相同。
大於零 這個執行個體晚於 value

CompareTo(DateTime)

比較這個執行個體的值與指定的 DateTime 值,並且傳回一個整數,指出這個執行個體是早於、同於或晚於指定的 DateTime 值。

public:
 virtual int CompareTo(DateTime value);
public int CompareTo (DateTime value);
abstract member CompareTo : DateTime -> int
override this.CompareTo : DateTime -> int
Public Function CompareTo (value As DateTime) As Integer

參數

value
DateTime

要與目前執行個體相比較的物件。

傳回

Int32

帶正負號的數字,指出這個執行個體與 value 參數的相對值。

描述
小於零 這個執行個體早於 value
這個執行個體和 value 相同。
大於零 這個執行個體晚於 value

實作

範例

下列範例會具現化三個 DateTime 物件,一個代表今天的日期,另一個代表前一年的日期,第三個代表未來的一年中的日期。 然後,它會呼叫 CompareTo(DateTime) 方法,並顯示比較的結果。

using System;

public class DateTimeComparison
{
   private enum DateComparisonResult
   {
      Earlier = -1,
      Later = 1,
      TheSame = 0
   };

   public static void Main()
   {
      DateTime thisDate = DateTime.Today;

      // Define two DateTime objects for today's date
      // next year and last year		
      DateTime thisDateNextYear, thisDateLastYear;

      // Call AddYears instance method to add/substract 1 year
      thisDateNextYear = thisDate.AddYears(1);
      thisDateLastYear = thisDate.AddYears(-1);

      // Compare dates
      //
      DateComparisonResult comparison;
      // Compare today to last year
      comparison = (DateComparisonResult) thisDate.CompareTo(thisDateLastYear);
      Console.WriteLine("CompareTo method returns {0}: {1:d} is {2} than {3:d}",
                        (int) comparison, thisDate, comparison.ToString().ToLower(),
                        thisDateLastYear);

      // Compare today to next year
      comparison = (DateComparisonResult) thisDate.CompareTo(thisDateNextYear);
      Console.WriteLine("CompareTo method returns {0}: {1:d} is {2} than {3:d}",
                        (int) comparison, thisDate, comparison.ToString().ToLower(),
                        thisDateNextYear);
   }
}
//
// If run on October 20, 2006, the example produces the following output:
//    CompareTo method returns 1: 10/20/2006 is later than 10/20/2005
//    CompareTo method returns -1: 10/20/2006 is earlier than 10/20/2007
Option Strict On

Module DateTimeComparison
   Private Enum DateComparisonResult
      Earlier = -1
      Later = 1
      TheSame = 0
   End Enum
   
   Public Sub Main()

      Dim thisDate As Date = Date.Today

      ' Define two DateTime objects for today's date 
      ' next year and last year		
      Dim thisDateNextYear, thisDateLastYear As Date

      ' Call AddYears instance method to add/substract 1 year
      thisDateNextYear = thisDate.AddYears(1)
      thisDateLastYear = thisDate.AddYears(-1)   

       
      ' Compare dates
      '
      Dim comparison As DateComparisonResult
      ' Compare today to last year
      comparison = CType(thisDate.CompareTo(thisDateLastYear), DateComparisonResult)
      Console.WriteLine("CompareTo method returns {0}: {1:d} is {2} than {3:d}", _ 
                        CInt(comparison), thisDate, comparison.ToString().ToLower(), _ 
                        thisDateLastYear)
      
      ' Compare today to next year
      comparison = CType(thisDate.CompareTo(thisDateNextYear), DateComparisonResult)
      Console.WriteLine("CompareTo method returns {0}: {1:d} is {2} than {3:d}", _
                        CInt(comparison), thisDate, comparison.ToString().ToLower(), _
                        thisDateNextYear)
   End Sub
End Module
'
' If run on October 20, 2006, the example produces the following output:
'    CompareTo method returns 1: 10/20/2006 is later than 10/20/2005
'    CompareTo method returns -1: 10/20/2006 is earlier than 10/20/2007

備註

若要判斷目前實例與的關聯性 value ,此 CompareTo 方法會比較 Ticks 目前實例的屬性, value 但是會忽略其 Kind 屬性。 在比較 DateTime 物件之前,請確定物件代表相同時區的時間。 您可以藉由比較其屬性的值來完成這項作業 Kind

這個方法會實作為 System.IComparable<T> 介面,而且執行方式稍微優於方法多載, DateTime.CompareTo(Object) 因為它不需要將 value 參數轉換成物件。

另請參閱

適用於

CompareTo(Object)

比較這個執行個體的值與含有指定之 DateTime 值的指定物件,並且傳回一個整數,指出這個執行個體是早於、同於或晚於指定的 DateTime 值。

public:
 virtual int CompareTo(System::Object ^ value);
public:
 int CompareTo(System::Object ^ value);
public int CompareTo (object? value);
public int CompareTo (object value);
abstract member CompareTo : obj -> int
override this.CompareTo : obj -> int
member this.CompareTo : obj -> int
Public Function CompareTo (value As Object) As Integer

參數

value
Object

要比較的 Boxed 物件,或 null

傳回

Int32

帶正負號的數字,其指出這個執行個體與 value 的相對值。

描述
小於零 這個執行個體早於 value
這個執行個體和 value 相同。
大於零 這個執行個體晚於 value,或者 valuenull

實作

例外狀況

value 不是 DateTime

範例

下列範例會示範 CompareTo 方法。

using namespace System;
void main()
{
   

   System::DateTime theDay(System::DateTime::Today.Year,7,28);
   int compareValue;
   try
   {
      compareValue = theDay.CompareTo( System::DateTime::Today );
   }
   catch ( ArgumentException^ ) 
   {
      System::Console::WriteLine( "Value is not a DateTime" );
      return;
   }

   if ( compareValue < 0 )
   {
      System::Console::WriteLine( "{0:d} is in the past.", theDay );
   }
   else
   if ( compareValue == 0 )
   {
      System::Console::WriteLine( "{0:d} is today!", theDay );
   }
   else
   // compareValue > 0 
   {
      System::Console::WriteLine( "{0:d} has not come yet.", theDay );
   }
}
System.DateTime theDay = new System.DateTime(System.DateTime.Today.Year, 7, 28);
int compareValue;

try
{
    compareValue = theDay.CompareTo(DateTime.Today);
}
catch (ArgumentException)
{
   Console.WriteLine("Value is not a DateTime");
   return;
}

if (compareValue < 0)
   System.Console.WriteLine("{0:d} is in the past.", theDay);
else if (compareValue == 0)
   System.Console.WriteLine("{0:d} is today!", theDay);
else // compareValue > 0
   System.Console.WriteLine("{0:d} has not come yet.", theDay);
Dim thDay As New System.DateTime(System.DateTime.Today.Year, 7, 28)

Dim compareValue As Integer
Try
   compareValue = thDay.CompareTo(System.DateTime.Today)
Catch exp As ArgumentException
   System.Console.WriteLine("Value is not a DateTime")
End Try

If compareValue < 0 Then
   System.Console.WriteLine("{0:d} is in the past.", thDay)
ElseIf compareValue = 0 Then
   System.Console.WriteLine("{0:d} is today!", thDay)
Else   ' compareValue >= 1 
   System.Console.WriteLine("{0:d} has not come yet.", thDay)
End If

備註

若要判斷目前實例與的關聯性 value ,此 CompareTo 方法會比較 Ticks 目前實例的屬性, value 但是會忽略其 Kind 屬性。 在比較 DateTime 物件之前,請確定物件代表相同時區的時間。 您可以藉由比較其屬性的值來完成這項作業 Kind

DateTime無論其值為何,都會將任何實例視為大於 null

另請參閱

適用於