Sunday, May 19, 2013

How to compare DateTime in C#

DateTime date1 = new DateTime(2008, 8, 1);
DateTime date2 = new DateTime(2011, 8, 1);
int result = DateTime.Compare(date1, date2);
string relationship;

if (result < 0)
   relationship = "is earlier than";
else if (result == 0)
   relationship = "is the same time as";         
else
   relationship = "is later than";

string = ("Date Compare :  " + date1 + " " + relationship + " " + date2);
 
// The example displays the following output: 
//    8/1/2008 is earlier than 8/1/2011 

No comments:

Post a Comment