DateTime.CompareTo DateTime.CompareTo DateTime.CompareTo DateTime.CompareTo Method

定义

将此实例的值与指定的 DateTime 值相比较,并指示此实例是早于、等于还是晚于指定的 DateTime 值。Compares the value of this instance to a specified DateTime value and indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.

重载

CompareTo(DateTime) CompareTo(DateTime) CompareTo(DateTime) CompareTo(DateTime)

将此实例的值与指定的 DateTime 值相比较,并返回一个整数,该整数指示此实例是早于、等于还是晚于指定的 DateTime 值。Compares the value of this instance to a specified DateTime value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.

CompareTo(Object) CompareTo(Object) CompareTo(Object) CompareTo(Object)

将此实例的值与包含指定的 DateTime 值的指定对象相比较,并返回一个整数,该整数指示此实例是早于、等于还是晚于指定的 DateTime 值。Compares the value of this instance to a specified object that contains a specified DateTime value, and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.

注解

CompareTo方法的两个重载返回一个有符号数字, 指示此实例value和参数的相对值, 如下表所示。The two overloads of the CompareTo method return a signed number that indicates the relative value of this instance and the value argument, as shown in the following table.

Value 说明Description
小于零Less than zero 此实例早于 valueThis instance is earlier than value.
Zero 此实例与 value 相同。This instance is the same as value.
大于零Greater than zero 此实例晚于 valueThis instance is later than value.

CompareTo(DateTime) CompareTo(DateTime) CompareTo(DateTime) CompareTo(DateTime)

将此实例的值与指定的 DateTime 值相比较,并返回一个整数,该整数指示此实例是早于、等于还是晚于指定的 DateTime 值。Compares the value of this instance to a specified DateTime value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.

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

要与当前类型进行比较的对象。The object to compare to the current instance.

返回

有符号数字,指示此实例和 value 参数的相对值。A signed number indicating the relative values of this instance and the value parameter.

Value 说明Description
小于零Less than zero 此实例早于 valueThis instance is earlier than value.
Zero 此实例与 value 相同。This instance is the same as value.
大于零Greater than zero 此实例晚于 valueThis instance is later than value.

实现

示例

下面的示例实例化DateTime三个对象, 一个表示当天的日期, 另一个表示一年中的日期, 另一个表示未来一年的日期。The following example instantiates three DateTime objects, one that represents today's date, another that represents the date one year previously, and a third that represents the date one year in the future. 然后, 它调用CompareTo(DateTime)方法并显示比较结果。It then calls the CompareTo(DateTime) method and displays the result of the comparison.

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属性。To determine the relationship of the current instance to value, the CompareTo method compares the Ticks property of the current instance and value but ignores their Kind property. 在比较DateTime对象之前, 请确保对象表示相同时区中的时间。Before comparing DateTime objects, make sure that the objects represent times in the same time zone. 可以通过比较其Kind属性的值来执行此操作。You can do this by comparing the values of their Kind properties.

此方法实现System.IComparable<T>接口, 且执行DateTime.CompareTo(Object)方式略优于方法重载, 因为value无需将参数转换为对象。This method implements the System.IComparable<T> interface and performs slightly better than the DateTime.CompareTo(Object) method overload because it does not have to convert the value parameter to an object.

另请参阅

CompareTo(Object) CompareTo(Object) CompareTo(Object) CompareTo(Object)

将此实例的值与包含指定的 DateTime 值的指定对象相比较,并返回一个整数,该整数指示此实例是早于、等于还是晚于指定的 DateTime 值。Compares the value of this instance to a specified object that contains a specified DateTime value, and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.

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

参数

value
Object Object Object Object

要比较的装箱对象,或 nullA boxed object to compare, or null.

返回

一个带符号数字,指示此实例和 value 的相对值。A signed number indicating the relative values of this instance and value.

Value 说明Description
小于零Less than zero 此实例早于 valueThis instance is earlier than value.
Zero 此实例与 value 相同。This instance is the same as value.
大于零Greater than zero 此实例晚于 value,或 valuenullThis instance is later than value, or value is null.

实现

异常

示例

下面的示例演示CompareTo方法。The following example demonstrates the CompareTo method.

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属性。To determine the relationship of the current instance to value, the CompareTo method compares the Ticks property of the current instance and value but ignores their Kind property. 在比较DateTime对象之前, 请确保对象表示相同时区中的时间。Before comparing DateTime objects, make sure that the objects represent times in the same time zone. 可以通过比较其Kind属性的值来执行此操作。You can do this by comparing the values of their Kind properties.

的任何实例DateTime, 无论其值如何, 都视为null大于。Any instance of DateTime, regardless of its value, is considered greater than null.

另请参阅

适用于