DateTime.Equals() Method in C#

The DateTime.Equals() method in C# is used to check whether two DateTime objects or instances are equal or not. It returns true if both are equal, otherwise false.

This method performs a tick-by-tick comparison, meaning it compares the exact moment in time down to the smallest unit (100-nanosecond intervals). Two DateTime objects are considered equal only if they represent the exact same point in time.

Syntax

Following is the syntax for the static DateTime.Equals() method −

public static bool Equals(DateTime date1, DateTime date2);

Parameters

  • date1 ? The first DateTime object to compare.

  • date2 ? The second DateTime object to compare.

Return Value

Returns true if both DateTime objects represent the same point in time, otherwise returns false.

Using DateTime.Equals() with Different Dates

Example

using System;
public class Demo {
    public static void Main() {
        DateTime d1 = new DateTime(2019, 09, 10, 5, 15, 25);
        DateTime d2 = d1.AddMonths(25);
        Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1);
        Console.WriteLine("\nNew DateTime (After adding months) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2);
        bool res = DateTime.Equals(d1, d2);
        if (res)
            Console.WriteLine("\ndate1 = date2. ");
        else
            Console.WriteLine("\ndate1 is not equal to date2. ");
    }
}

The output of the above code is −

Initial DateTime = 10 September 2019, 05:15:25
New DateTime (After adding months) = 10 October 2021, 05:15:25
date1 is not equal to date2.

Using DateTime.Equals() with Identical Dates

Example

using System;
public class Demo {
    public static void Main() {
        DateTime d1 = new DateTime(2019, 11, 11, 7, 15, 30);
        DateTime d2 = new DateTime(2019, 11, 11, 7, 15, 30);
        Console.WriteLine("DateTime1 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1);
        Console.WriteLine("\nDateTime2 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2);
        bool res = DateTime.Equals(d1, d2);
        if (res)
            Console.WriteLine("\ndate1 = date2 ");
        else
            Console.WriteLine("\ndate1 is not equal to date2 ");
    }
}

The output of the above code is −

DateTime1 = 11 November 2019, 07:15:30
DateTime2 = 11 November 2019, 07:15:30
date1 = date2

Instance Method vs Static Method

The DateTime class also provides an instance method version of Equals()

Example

using System;
public class Demo {
    public static void Main() {
        DateTime d1 = new DateTime(2023, 5, 15, 10, 30, 0);
        DateTime d2 = new DateTime(2023, 5, 15, 10, 30, 0);
        DateTime d3 = new DateTime(2023, 5, 15, 10, 30, 1);
        
        // Using static method
        Console.WriteLine("Static method: d1.Equals(d2) = " + DateTime.Equals(d1, d2));
        
        // Using instance method
        Console.WriteLine("Instance method: d1.Equals(d2) = " + d1.Equals(d2));
        Console.WriteLine("Instance method: d1.Equals(d3) = " + d1.Equals(d3));
    }
}

The output of the above code is −

Static method: d1.Equals(d2) = True
Instance method: d1.Equals(d2) = True
Instance method: d1.Equals(d3) = False

Conclusion

The DateTime.Equals() method provides precise comparison of two DateTime objects down to the tick level. It's available both as a static method and an instance method, making it flexible for different coding scenarios when you need to determine if two dates represent exactly the same moment in time.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements