DateTime.Compare Method
.NET Framework 3.0
Compares two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
public static int Compare ( DateTime t1, DateTime t2 )
public static function Compare ( t1 : DateTime, t2 : DateTime ) : int
Not applicable.
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 earlier than t2. |
| Zero | t1 is the same as t2. |
| Greater than zero | t1 is later than t2. |
The following code example demonstrates Compare.
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" );
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");
}
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");
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show: