DateTime.Equals Method

Definition

Returns a value indicating whether two DateTime objects, or a DateTime instance and another object or DateTime, have the same value.

Overloads

Equals(DateTime)

Returns a value indicating whether the value of this instance is equal to the value of the specified DateTime instance.

Equals(Object)

Returns a value indicating whether this instance is equal to a specified object.

Equals(DateTime, DateTime)

Returns a value indicating whether two DateTime instances have the same date and time value.

Equals(DateTime)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Returns a value indicating whether the value of this instance is equal to the value of the specified DateTime instance.

C#
public bool Equals(DateTime value);

Parameters

value
DateTime

The object to compare to this instance.

Returns

true if the value parameter equals the value of this instance; otherwise, false.

Implements

Examples

The following example demonstrates the Equals method.

C#
using System;

public class Application
{
    public static void Main()
    {
        // Create some DateTime objects.
        DateTime one = DateTime.UtcNow;

        DateTime two = DateTime.Now;

        DateTime three = one;

        // Compare the DateTime objects and display the results.
        bool result = one.Equals(two);

        Console.WriteLine("The result of comparing DateTime object one and two is: {0}.", result);

        result = one.Equals(three);

        Console.WriteLine("The result of comparing DateTime object one and three is: {0}.", result);
    }
}

// This code example displays the following:
//
// The result of comparing DateTime object one and two is: False.
// The result of comparing DateTime object one and three is: True.

Remarks

The current instance and value are equal if their Ticks property values are equal. Their Kind property values are not considered in the test for equality.

This method implements the System.IEquatable<T> interface, and performs slightly better than the Equals method because the value parameter does not have to be converted to an object.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Equals(Object)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Returns a value indicating whether this instance is equal to a specified object.

C#
public override bool Equals(object value);
C#
public override bool Equals(object? value);

Parameters

value
Object

The object to compare to this instance.

Returns

true if value is an instance of DateTime and equals the value of this instance; otherwise, false.

Examples

The following example demonstrates the Equals method.

C#
using System;

public class Application
{
    public static void Main()
    {
        // Create some DateTime objects.
        DateTime one = DateTime.UtcNow;

        DateTime two = DateTime.Now;

        DateTime three = one;

        // Compare the DateTime objects and display the results.
        bool result = one.Equals(two);

        Console.WriteLine("The result of comparing DateTime object one and two is: {0}.", result);

        result = one.Equals(three);

        Console.WriteLine("The result of comparing DateTime object one and three is: {0}.", result);
    }
}

// This code example displays the following:
//
// The result of comparing DateTime object one and two is: False.
// The result of comparing DateTime object one and three is: True.

Remarks

The current instance and value are equal if their Ticks property values are equal. Their Kind property values are not considered in the test for equality.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Equals(DateTime, DateTime)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Returns a value indicating whether two DateTime instances have the same date and time value.

C#
public static bool Equals(DateTime t1, DateTime t2);

Parameters

t1
DateTime

The first object to compare.

t2
DateTime

The second object to compare.

Returns

true if the two values are equal; otherwise, false.

Examples

The following example demonstrates the Equals method.

C#
System.DateTime today1 =
        new System.DateTime(System.DateTime.Today.Ticks);
System.DateTime today2 =
        new System.DateTime(System.DateTime.Today.Ticks);
System.DateTime tomorrow =
        new System.DateTime(
                    System.DateTime.Today.AddDays(1).Ticks);

// todayEqualsToday gets true.
bool todayEqualsToday = System.DateTime.Equals(today1, today2);

// todayEqualsTomorrow gets false.
bool todayEqualsTomorrow = System.DateTime.Equals(today1, tomorrow);

Remarks

t1 and t2 are equal if their Ticks property values are equal. Their Kind property values are not considered in the test for equality.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0