Export (0) Print
Expand All
Expand Minimize

DateTime.Compare Method

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)

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

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

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft