Comparing dates using C#

Comparing dates in C# is accomplished using the DateTime structure, which provides built-in comparison operators and methods. The DateTime class allows you to compare dates using standard operators like <, >, ==, and specialized methods like Compare().

Syntax

Following is the syntax for creating DateTime objects for comparison −

DateTime date1 = new DateTime(year, month, day);
DateTime date2 = new DateTime(year, month, day);

Following are the comparison operators and methods available −

// Using comparison operators
if (date1 < date2) { }
if (date1 > date2) { }
if (date1 == date2) { }

// Using Compare method
int result = DateTime.Compare(date1, date2);

Using Comparison Operators

The simplest way to compare dates is using standard comparison operators −

using System;

class Program {
   static void Main() {
      DateTime date1 = new DateTime(2018, 07, 20);
      DateTime date2 = new DateTime(2018, 07, 25);
      
      Console.WriteLine("Date 1: {0}", date1.ToShortDateString());
      Console.WriteLine("Date 2: {0}", date2.ToShortDateString());
      
      if (date1 < date2)
         Console.WriteLine("{0} comes before {1}", date1.ToShortDateString(), date2.ToShortDateString());
      else if (date1 > date2)
         Console.WriteLine("{0} comes after {1}", date1.ToShortDateString(), date2.ToShortDateString());
      else
         Console.WriteLine("Both dates are equal");
   }
}

The output of the above code is −

Date 1: 7/20/2018
Date 2: 7/25/2018
7/20/2018 comes before 7/25/2018

Using DateTime.Compare() Method

The DateTime.Compare() method returns an integer value indicating the relationship between two dates −

using System;

class Program {
   static void Main() {
      DateTime date1 = new DateTime(2023, 12, 15);
      DateTime date2 = new DateTime(2023, 12, 10);
      DateTime date3 = new DateTime(2023, 12, 15);
      
      int result1 = DateTime.Compare(date1, date2);
      int result2 = DateTime.Compare(date1, date3);
      int result3 = DateTime.Compare(date2, date1);
      
      Console.WriteLine("Comparing {0} with {1}: {2}", date1.ToShortDateString(), date2.ToShortDateString(), result1);
      Console.WriteLine("Comparing {0} with {1}: {2}", date1.ToShortDateString(), date3.ToShortDateString(), result2);
      Console.WriteLine("Comparing {0} with {1}: {2}", date2.ToShortDateString(), date1.ToShortDateString(), result3);
   }
}

The output of the above code is −

Comparing 12/15/2023 with 12/10/2023: 1
Comparing 12/15/2023 with 12/15/2023: 0
Comparing 12/10/2023 with 12/15/2023: -1

Return Values of DateTime.Compare()

Return Value Meaning
Less than 0 First date is earlier than second date
0 Both dates are equal
Greater than 0 First date is later than second date

Using CompareTo() Method

You can also use the CompareTo() method which is an instance method of DateTime −

using System;

class Program {
   static void Main() {
      DateTime today = DateTime.Today;
      DateTime yesterday = DateTime.Today.AddDays(-1);
      DateTime tomorrow = DateTime.Today.AddDays(1);
      
      Console.WriteLine("Today: {0}", today.ToShortDateString());
      Console.WriteLine("Yesterday: {0}", yesterday.ToShortDateString());
      Console.WriteLine("Tomorrow: {0}", tomorrow.ToShortDateString());
      
      Console.WriteLine("Today compared to yesterday: {0}", today.CompareTo(yesterday));
      Console.WriteLine("Today compared to tomorrow: {0}", today.CompareTo(tomorrow));
      Console.WriteLine("Today compared to today: {0}", today.CompareTo(today));
   }
}

The output of the above code is −

Today: 12/15/2024
Yesterday: 12/14/2024
Tomorrow: 12/16/2024
Today compared to yesterday: 1
Today compared to tomorrow: -1
Today compared to today: 0

Practical Example: Finding the Latest Date

using System;

class Program {
   static void Main() {
      DateTime[] dates = {
         new DateTime(2023, 5, 15),
         new DateTime(2023, 8, 22),
         new DateTime(2023, 3, 10),
         new DateTime(2023, 11, 5)
      };
      
      DateTime latest = dates[0];
      DateTime earliest = dates[0];
      
      foreach (DateTime date in dates) {
         if (date > latest)
            latest = date;
         if (date < earliest)
            earliest = date;
      }
      
      Console.WriteLine("Earliest date: {0}", earliest.ToShortDateString());
      Console.WriteLine("Latest date: {0}", latest.ToShortDateString());
   }
}

The output of the above code is −

Earliest date: 3/10/2023
Latest date: 11/5/2023

Conclusion

C# provides multiple ways to compare dates using the DateTime structure. You can use comparison operators (<, >, ==) for simple comparisons, or methods like Compare() and CompareTo() when you need numerical comparison results for sorting or conditional logic.

Updated on: 2026-03-17T07:04:35+05:30

384 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements