DateTime.CompareTo() Method in C#

The DateTime.CompareTo() method in C# is used to compare the value of this instance to a specified DateTime value. It returns an integer indicating the chronological relationship between two DateTime objects.

Syntax

Following is the syntax −

public int CompareTo(DateTime value);

Parameters

value − The DateTime object to compare with the current instance.

Return Value

It returns an integer value −

  • < 0 − If this instance is earlier than the specified value
  • 0 − If this instance is the same as the specified value
  • > 0 − If this instance is later than the specified value

DateTime.CompareTo() Return Values Less than 0 Earlier date date1 < date2 Equal to 0 Same date date1 = date2 Greater than 0 Later date date1 > date2 Timeline Past Now Future

Using CompareTo() with Equal Dates

Example

using System;

public class Demo {
   public static void Main() {
      DateTime date1 = new DateTime(2019, 05, 20, 6, 20, 40);
      DateTime date2 = new DateTime(2019, 05, 20, 6, 20, 40);
      
      Console.WriteLine("DateTime 1 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", date1);
      Console.WriteLine("DateTime 2 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", date2);
      
      int res = date1.CompareTo(date2);
      Console.WriteLine("Comparison Result: " + res);
      
      if (res == 0)
         Console.WriteLine("Both dates are equal");
   }
}

The output of the above code is −

DateTime 1 = 20 May 2019, 06:20:40
DateTime 2 = 20 May 2019, 06:20:40
Comparison Result: 0
Both dates are equal

Using CompareTo() with Different Dates

Example

using System;

public class Demo {
   public static void Main() {
      DateTime date1 = new DateTime(2019, 08, 20, 6, 20, 40);
      DateTime date2 = new DateTime(2019, 05, 20, 6, 20, 40);
      
      Console.WriteLine("DateTime 1 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", date1);
      Console.WriteLine("DateTime 2 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", date2);
      
      int res = date1.CompareTo(date2);
      Console.WriteLine("Comparison Result: " + res);
      
      if (res > 0)
         Console.WriteLine("Date1 is later than Date2");
      else if (res < 0)
         Console.WriteLine("Date1 is earlier than Date2");
      else
         Console.WriteLine("Both dates are equal");
   }
}

The output of the above code is −

DateTime 1 = 20 August 2019, 06:20:40
DateTime 2 = 20 May 2019, 06:20:40
Comparison Result: 1
Date1 is later than Date2

Using CompareTo() for Sorting

Example

using System;

public class Demo {
   public static void Main() {
      DateTime[] dates = {
         new DateTime(2023, 12, 25),
         new DateTime(2023, 01, 15),
         new DateTime(2023, 06, 30)
      };
      
      Console.WriteLine("Original dates:");
      foreach (DateTime date in dates) {
         Console.WriteLine(date.ToString("yyyy-MM-dd"));
      }
      
      Array.Sort(dates);
      
      Console.WriteLine("\nSorted dates:");
      foreach (DateTime date in dates) {
         Console.WriteLine(date.ToString("yyyy-MM-dd"));
      }
   }
}

The output of the above code is −

Original dates:
2023-12-25
2023-01-15
2023-06-30

Sorted dates:
2023-01-15
2023-06-30
2023-12-25

Conclusion

The DateTime.CompareTo() method provides a reliable way to compare DateTime objects chronologically. It returns negative, zero, or positive integers based on whether the current instance is earlier, equal, or later than the specified DateTime value, making it useful for sorting and conditional operations.

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements