Thursday, April 9, 2009

Date Compare in C#

Visual Basic

Dim t1 As DateTime
Dim t2 As DateTime
Dim returnValue As Integer

returnValue = DateTime.Compare(t1, t2)


C#
public static int Compare(
DateTime t1,
DateTime t2
)

Parameters
t1
Type: System..::.DateTime
The first DateTime.

t2
Type: System..::.DateTime
The second DateTime.

Return Value
Type: System..::.Int32
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.

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

No comments: