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.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var