DateTime.CompareTo Method (Object)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
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.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.Object
The object to compare to this instance, or Nothing.
Return Value
Type: System.Int32A signed integer that indicates the relationship between this instance and the value parameter, 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, or value is Nothing. |
Implements
IComparable.CompareTo(Object)| Exception | Condition |
|---|---|
| ArgumentException | value is not a DateTime. |
The following example demonstrates the CompareTo method.
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 outputBlock.Text += "Value is not a DateTime" + vbCrLf End Try If compareValue < 0 Then outputBlock.Text += String.Format("{0:d} is in the past.", thDay) + vbCrLf ElseIf compareValue = 0 Then outputBlock.Text += String.Format("{0:d} is today!", thDay) + vbCrLf Else ' compareValue >= 1 outputBlock.Text += String.Format("{0:d} has not come yet.", thDay) + vbCrLf End If