Tuesday, 8 July 2008

Compare two DateTime

This is how you can compare two DateTime:

DateTime dateTime1 = new DateTime(2007, 04, 20);
DateTime dateTime2 = new DateTime(2008, 01, 20);

if (DateTime.Compare(dateTime1, dateTime2) < 0)
{
Console.WriteLine("DateTime2 is larger than dateTime1");
}
else if (DateTime.Compare(dateTime1, dateTime2) == 0)
{
Console.WriteLine("Dates are equal");
}
else if (DateTime.Compare(dateTime1, dateTime2) > 0)
{
Console.WriteLine("DateTime1 is larger than dateTime2");
}

No comments: