Compare object to DateTime and returns an integer that indicates the sequence in CSharp
Description
The following code shows how to compare object to DateTime and returns an integer that indicates the sequence.
Example
using System;/*from w ww . j a v a2 s. c o m*/
public class MainClass{
public static void Main(String[] argv){
System.DateTime theDay = new System.DateTime(System.DateTime.Today.Year, 7, 28);
int compareValue;
try {
compareValue = theDay.CompareTo(DateTime.Today);
} catch (ArgumentException) {
Console.WriteLine("Value is not a DateTime");
return;
}
if (compareValue < 0)
System.Console.WriteLine("{0:d} is in the past.", theDay);
else if (compareValue == 0)
System.Console.WriteLine("{0:d} is today!", theDay);
else // compareValue > 0
System.Console.WriteLine("{0:d} has not come yet.", theDay);
}
}
The code above generates the following result.