CompareTo Method (DateTime)
Collapse the table of content
Expand the table of content

DateTime::CompareTo Method (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.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

virtual int CompareTo(
	DateTime value
) sealed


Type: System::DateTime

The object to compare to the current instance.

Return Value

Type: System::Int32

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.

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.

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.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft