Share via


DateTime.CompareTo Method (Object)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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.

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

Syntax

'Declaration
Public Function CompareTo ( _
    value As Object _
) As Integer
public int CompareTo(
    Object value
)

Parameters

  • value
    Type: System.Object
    The object to compare to this instance, or nulla null reference (Nothing in Visual Basic).

Return Value

Type: System.Int32
A 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 nulla null reference (Nothing in Visual Basic).

Implements

IComparable.CompareTo(Object)

Exceptions

Exception Condition
ArgumentException

value is not a DateTime.

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

Any instance of DateTime, regardless of its value, is considered greater than nulla null reference (Nothing in Visual Basic).

Examples

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
System.DateTime theDay = new System.DateTime(System.DateTime.Today.Year, 7, 28);
int compareValue;

try
{
   compareValue = theDay.CompareTo(DateTime.Today);
}
catch (ArgumentException)
{
   outputBlock.Text += "Value is not a DateTime" + "\n";
   return;
}

if (compareValue < 0)
   outputBlock.Text += String.Format("{0:d} is in the past.", theDay) + "\n";
else if (compareValue == 0)
   outputBlock.Text += String.Format("{0:d} is today!", theDay) + "\n";
else // compareValue > 0
   outputBlock.Text += String.Format("{0:d} has not come yet.", theDay) + "\n";

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.