System Namespace DateTime Structure
Compares this instance to a specified object and returns an indication of their relative values.
[ VB ]
<Serializable>
NotOverridable Public Function CompareTo ( _
ByVal value As Object _
} As Integer
[ C# ]
[ Serializable ]
public int CompareTo (
object value
};
[ C++ ]
[ Serializable ]
public: __sealed int CompareTo (
Object* value
};
[ JScript ]
public Serializable
function CompareTo (
value : Object
} : int;
- value
- An object to compare, or a null reference ( Nothing in Visual Basic ).
A signed number indicating the relative values of this instance and value.
Value |
Description |
Less than zero |
This instance is less than value. |
Zero |
This instance is equal to value. |
Greater than zero |
This instance is greater than value, or value is a null reference ( Nothing in Visual Basic ). |
Any instance of DateTime, regardless of its value, is considered greater than a null reference ( Nothing in Visual Basic ).
value must be an instance of DateTime or a null reference ( Nothing ); otherwise, an exception is thrown.
The below code snippet demonstrates using the CompareTo method.
System.DateTime theDay =
new System.DateTime ( System.DateTime.Today.Year, 7, 28 );
int compareValue;
try {
compareValue = theDay.CompareTo ( System.DateTime.Today );
}
catch ( ArgumentException ) {
System.Response.WriteLine ( "Value is not a DateTime" );
return;
}
if ( compareValue < 0 ) {
System.Response.WriteLine ( "{ 0:d } is in the past.", theDay );
}
else if ( compareValue == 0 ) {
System.Response.WriteLine ( "{ 0:d } is today!", theDay );
}
else if ( compareValue == 1 ) {
System.Response.WriteLine ( "Value is null" );
}
// compareValue > 0 && compareValue != 1
else {
System.Response.WriteLine ( "{ 0:d } has not come yet.", theDay );
}
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
System.Response.WriteLine ( "Value is not a DateTime" )
End Try
If compareValue < 0 Then
System.Response.WriteLine ( "{ 0:d } is in the past.", thDay )
ElseIf compareValue = 0 Then
System.Response.WriteLine ( "{ 0:d } is today!", thDay )
ElseIf compareValue = 1 Then
System.Response.WriteLine ( "Value is null" )
Else ' compareValue > 0 && compareValue != 1
System.Response.WriteLine ( "{ 0:d } has not come yet.", thDay )
End If |
|
C# |
VB |
DateTime Members Compare Equals