System.DateTime.CompareTo Method

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.

Syntax

public int CompareTo (object value)

Parameters

value
A boxed object to compare, or null.

Returns

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

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.

Exceptions

TypeReason
ArgumentException value is not a DateTime and is not a null reference.

Remarks

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.

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

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0