How to compare two Dates in C#?

To compare dates in C#, you need to first set two dates to be compared using the DateTime object. The DateTime class in C# provides several methods and operators for date comparison including direct comparison operators, the CompareTo() method, and specialized methods like Equals().

Syntax

Following are the different syntaxes for comparing dates −

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

// Using CompareTo method
int result = date1.CompareTo(date2);

// Using Equals method
bool isEqual = date1.Equals(date2);

Using Comparison Operators

The simplest way to compare dates is using standard comparison operators (<, >, ==, !=, <=, >=) −

Example

using System;

class Demo {
    static void Main() {
        DateTime date1 = new DateTime(2018, 08, 05);
        Console.WriteLine("Date 1 : {0}", date1.ToString("yyyy-MM-dd"));

        DateTime date2 = new DateTime(2018, 08, 07);
        Console.WriteLine("Date 2 : {0}", date2.ToString("yyyy-MM-dd"));

        if (date1 < date2)
            Console.WriteLine("{0} comes before {1}", date1.ToString("yyyy-MM-dd"), date2.ToString("yyyy-MM-dd"));
        else if (date1 > date2)
            Console.WriteLine("{0} comes after {1}", date1.ToString("yyyy-MM-dd"), date2.ToString("yyyy-MM-dd"));
        else
            Console.WriteLine("Both dates are equal");
    }
}

The output of the above code is −

Date 1 : 2018-08-05
Date 2 : 2018-08-07
2018-08-05 comes before 2018-08-07

Using CompareTo() Method

The CompareTo() method returns an integer value indicating the relationship between two dates −

Example

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);

        Console.WriteLine("Date1: {0}", date1.ToString("yyyy-MM-dd"));
        Console.WriteLine("Date2: {0}", date2.ToString("yyyy-MM-dd"));
        Console.WriteLine("Date3: {0}", date3.ToString("yyyy-MM-dd"));

        int result1 = date1.CompareTo(date2);
        int result2 = date1.CompareTo(date3);
        int result3 = date2.CompareTo(date1);

        Console.WriteLine("\nCompareTo Results:");
        Console.WriteLine("date1.CompareTo(date2): {0}", result1);
        Console.WriteLine("date1.CompareTo(date3): {0}", result2);
        Console.WriteLine("date2.CompareTo(date1): {0}", result3);
    }
}

The output of the above code is −

Date1: 2023-12-15
Date2: 2023-12-10
Date3: 2023-12-15

CompareTo Results:
date1.CompareTo(date2): 1
date1.CompareTo(date3): 0
date2.CompareTo(date1): -1

Return Value

The CompareTo() method returns −

  • Positive value (1) − when the first date is later than the second date

  • Zero (0) − when both dates are equal

  • Negative value (-1) − when the first date is earlier than the second date

Using DateTime.Compare() Static Method

You can also use the static DateTime.Compare() method for comparison −

Example

using System;

class CompareExample {
    static void Main() {
        DateTime startDate = new DateTime(2024, 1, 1);
        DateTime endDate = new DateTime(2024, 12, 31);
        DateTime currentDate = DateTime.Now;

        Console.WriteLine("Start Date: {0}", startDate.ToString("yyyy-MM-dd"));
        Console.WriteLine("End Date: {0}", endDate.ToString("yyyy-MM-dd"));
        Console.WriteLine("Current Date: {0}", currentDate.ToString("yyyy-MM-dd"));

        int compareResult = DateTime.Compare(currentDate, startDate);
        if (compareResult < 0)
            Console.WriteLine("Current date is before start date");
        else if (compareResult == 0)
            Console.WriteLine("Current date equals start date");
        else
            Console.WriteLine("Current date is after start date");

        // Check if current date is within range
        if (DateTime.Compare(currentDate, startDate) >= 0 && DateTime.Compare(currentDate, endDate) <= 0)
            Console.WriteLine("Current date is within the specified range");
        else
            Console.WriteLine("Current date is outside the specified range");
    }
}

The output of the above code is −

Start Date: 2024-01-01
End Date: 2024-12-31
Current Date: 2024-12-19
Current date is after start date
Current date is within the specified range

Comparison Methods Summary

Method Return Type Description
Comparison Operators (<, >, ==) bool Simple true/false comparison
CompareTo() int Returns -1, 0, or 1 based on relationship
DateTime.Compare() int Static method with same return values as CompareTo()
Equals() bool Checks for exact equality

Conclusion

Comparing dates in C# can be accomplished using comparison operators for simple comparisons, or the CompareTo() and DateTime.Compare() methods when you need more detailed comparison results. Choose the method that best fits your specific comparison requirements.

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements