System.DateTime.CompareTo Method

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 int CompareTo (DateTime value)


The object to compare to the current instance.


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

Less than zero

This instance is earlier than value.


This instance is the same as value.

Greater than zero

This instance is later than value.


This method implements the IComparable`1 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.

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 DateTime.Kind properties.


Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions:,
Since: .NET 2.0