Search This Blog

Wednesday, April 9, 2014

DateTime Compare C# Example

C# > System Namespace > DateTime > Compare

Compares two dates of type DateTime.

Example


DateTime date1 = new DateTime(2014, 4, 1, 0, 0, 0);
DateTime date2 = new DateTime(2014, 5, 1, 0, 0, 0);
int cmp = DateTime.Compare(date1, date2);
if (cmp < 0)
MessageBox.Show("date1 is earlier than date2");
else if (cmp == 0)
       MessageBox.Show("date1 is equal to date2");
else
       MessageBox.Show("date1 is later than date2");