Windows Dev Center

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DateTime.Compare Method

Compares two instances of DateTime and returns an indication of their relative values.

[Visual Basic]
Public Shared Function Compare( _
   ByVal t1 As DateTime, _
   ByVal t2 As DateTime _
) As Integer
[C#]
public static int Compare(
 DateTime t1,
 DateTime t2
);
[C++]
public: static int Compare(
 DateTime t1,
 DateTime t2
);
[JScript]
public static function Compare(
   t1 : DateTime,
 t2 : DateTime
) : int;

Parameters

t1
The first DateTime.
t2
The second DateTime.

Return Value

A signed number indicating the relative values of t1 and t2.

Value Type Condition
Less than zero t1 is less than t2.
Zero t1 equals t2.
Greater than zero t1 is greater than t2.

Example

The following sample demonstrates Compare.

[Visual Basic] 
Dim t1 As New DateTime(100)
Dim t2 As New DateTime(20)

If DateTime.Compare(t1, t2) > 0 Then
    Console.WriteLine("t1 > t2")
End If
If DateTime.Compare(t1, t2) = 0 Then
    Console.WriteLine("t1 == t2")
End If
If DateTime.Compare(t1, t2) < 0 Then
    Console.WriteLine("t1 < t2")
End If

[C#] 
DateTime t1 = new DateTime(100);
DateTime t2 = new DateTime(20);

if (DateTime.Compare(t1, t2) >  0) Console.WriteLine("t1 > t2"); 
if (DateTime.Compare(t1, t2) == 0) Console.WriteLine("t1 == t2"); 
if (DateTime.Compare(t1, t2) <  0) Console.WriteLine("t1 < t2");

[C++] 

 DateTime t1(100);
 DateTime t2(20);
 
 if (DateTime::Compare(t1, t2) >  0) Console::WriteLine("t1 > t2"); 
 if (DateTime::Compare(t1, t2) == 0) Console::WriteLine("t1 == t2"); 
 if (DateTime::Compare(t1, t2) <  0) Console::WriteLine("t1 < t2");

[JScript] 
var t1 : DateTime = new DateTime(100);
var t2 : DateTime = new DateTime(20);

if (DateTime.Compare(t1, t2) >  0) Console.WriteLine("t1 > t2"); 
if (DateTime.Compare(t1, t2) == 0) Console.WriteLine("t1 == t2"); 
if (DateTime.Compare(t1, t2) <  0) Console.WriteLine("t1 < t2");

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

DateTime Structure | DateTime Members | System Namespace | CompareTo | Equals

Show:
© 2015 Microsoft