DateTime.CompareTo Method

Definition

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.

Overloads

CompareTo(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)

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.

Remarks

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 This instance is earlier than value.
Zero This instance is the same as value.
Greater than zero This instance is later than value.

CompareTo(DateTime)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

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.

C#
public int CompareTo (DateTime value);

Parameters

value
DateTime

The object to compare to the current instance.

Returns

A signed number indicating the relative values of this instance and the value parameter.

Value Description
Less than zero This instance is earlier than value.
Zero This instance is the same as value.
Greater than zero This instance is later than value.

Implements

Examples

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. It then calls the CompareTo(DateTime) method and displays the result of the comparison.

C#
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

Remarks

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. Before comparing DateTime objects, make sure that the objects represent times in the same time zone. You can do this by comparing the values of their Kind properties.

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.

See also

Applies to

.NET 9 и другие версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

CompareTo(Object)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

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.

C#
public int CompareTo (object? value);
C#
public int CompareTo (object value);

Parameters

value
Object

A boxed object to compare, or null.

Returns

A signed number indicating the relative values of this instance and value.

Value Description
Less than zero This instance is earlier than value.
Zero This instance is the same as value.
Greater than zero This instance is later than value, or value is null.

Implements

Exceptions

value is not a DateTime.

Examples

The following example demonstrates the CompareTo method.

C#
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);

Remarks

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. Before comparing DateTime objects, make sure that the objects represent times in the same time zone. You can do this by comparing the values of their Kind properties.

Any instance of DateTime, regardless of its value, is considered greater than null.

See also

Applies to

.NET 9 и другие версии
Продукт Версии
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1