Export (0) Print
Expand All
Expand Minimize

DateTime.Compare Method

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

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

public static int Compare (
	DateTime t1,
	DateTime t2
)
public static int Compare (
	DateTime t1, 
	DateTime t2
)
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.

Before comparing DateTime objects, insure that the objects represent times in the same time zone.

The following code example demonstrates Compare.

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");

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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft