DateTimeOffset.Equals Method

Definition

Determines whether two DateTimeOffset objects are equal, or a DateTimeOffset object is equal to a specified object.

Overloads

Equals(DateTimeOffset)

Determines whether the current DateTimeOffset object represents the same point in time as a specified DateTimeOffset object.

Equals(Object)

Determines whether a DateTimeOffset object represents the same point in time as a specified object.

Equals(DateTimeOffset, DateTimeOffset)

Determines whether two specified DateTimeOffset objects represent the same point in time.

Equals(DateTimeOffset)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

Determines whether the current DateTimeOffset object represents the same point in time as a specified DateTimeOffset object.

C#
public bool Equals(DateTimeOffset other);

Parameters

other
DateTimeOffset

An object to compare to the current DateTimeOffset object.

Returns

true if both DateTimeOffset objects have the same UtcDateTime value; otherwise, false.

Implements

Examples

The following example illustrates calls to the Equals(DateTimeOffset) method to test DateTimeOffset objects for equality with the current DateTimeOffset object.

C#
private static void CompareForEquality1()
{
   DateTimeOffset firstTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0,
                              new TimeSpan(-7, 0, 0));

   DateTimeOffset secondTime = firstTime;
   Console.WriteLine("{0} = {1}: {2}",
                     firstTime, secondTime,
                     firstTime.Equals(secondTime));

   secondTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0,
                    new TimeSpan(-6, 0, 0));
   Console.WriteLine("{0} = {1}: {2}",
                    firstTime, secondTime,
                    firstTime.Equals(secondTime));

   secondTime = new DateTimeOffset(2007, 9, 1, 8, 45, 0,
                    new TimeSpan(-5, 0, 0));
   Console.WriteLine("{0} = {1}: {2}",
                    firstTime, secondTime,
                    firstTime.Equals(secondTime));
   // The example displays the following output to the console:
   //      9/1/2007 6:45:00 AM -07:00 = 9/1/2007 6:45:00 AM -07:00: True
   //      9/1/2007 6:45:00 AM -07:00 = 9/1/2007 6:45:00 AM -06:00: False
   //      9/1/2007 6:45:00 AM -07:00 = 9/1/2007 8:45:00 AM -05:00: True

Remarks

Before it performs the comparison, this method converts the values of both DateTimeOffset objects to Coordinated Universal Time (UTC). The method is equivalent to the following:

C#
return this.UtcDateTime == other.UtcDateTime;

In other words, the Equals(DateTimeOffset) method determines whether two DateTimeOffset objects represent a single point in time. It directly compares neither dates and times nor offsets. To determine whether two DateTimeOffset objects represent the same time and have the same offset value, use the EqualsExact method.

A DateTimeOffset object that is not null is considered to be later (or greater) than one that is null.

This overload of the Equals(DateTimeOffset) method implements the IEquatable<T>.Equals method. It offers slightly better performance than the DateTimeOffset.Equals(Object) overload because the other parameter does not have to be converted from 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:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

Determines whether a DateTimeOffset object represents the same point in time as a specified object.

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

Parameters

obj
Object

The object to compare to the current DateTimeOffset object.

Returns

true if the obj parameter is a DateTimeOffset object and represents the same point in time as the current DateTimeOffset object; otherwise, false.

Examples

The following example indicates whether the current DateTimeOffset object is equal to several other DateTimeOffset objects, as well as to a null reference and a DateTime object.

C#
private static void CompareForEquality2()
{
   DateTimeOffset firstTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0,
                              new TimeSpan(-7, 0, 0));

   object secondTime = firstTime;
   Console.WriteLine("{0} = {1}: {2}",
                     firstTime, secondTime,
                     firstTime.Equals(secondTime));

   secondTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0,
                    new TimeSpan(-6, 0, 0));
   Console.WriteLine("{0} = {1}: {2}",
                    firstTime, secondTime,
                    firstTime.Equals(secondTime));

   secondTime = new DateTimeOffset(2007, 9, 1, 8, 45, 0,
                    new TimeSpan(-5, 0, 0));
   Console.WriteLine("{0} = {1}: {2}",
                    firstTime, secondTime,
                    firstTime.Equals(secondTime));

   secondTime = null;
   Console.WriteLine("{0} = {1}: {2}",
                    firstTime, secondTime,
                    firstTime.Equals(secondTime));

   secondTime = new DateTime(2007, 9, 1, 6, 45, 00);
   Console.WriteLine("{0} = {1}: {2}",
                    firstTime, secondTime,
                    firstTime.Equals(secondTime));
   // The example displays the following output to the console:
   //       9/1/2007 6:45:00 AM -07:00 = 9/1/2007 6:45:00 AM -07:00: True
   //       9/1/2007 6:45:00 AM -07:00 = 9/1/2007 6:45:00 AM -06:00: False
   //       9/1/2007 6:45:00 AM -07:00 = 9/1/2007 8:45:00 AM -05:00: True
   //       9/1/2007 6:45:00 AM -07:00 = : False
   //       9/1/2007 6:45:00 AM -07:00 = 9/1/2007 6:45:00 AM: False

Remarks

Before it performs the comparison, this method converts the values of both the current DateTimeOffset object and the obj parameter to Coordinated Universal Time (UTC). The method is equivalent to the following:

C#
return this.UtcDateTime == ((DateTimeOffset) obj).UtcDateTime;

In other words, the DateTimeOffset.Equals(Object) method determines whether the current DateTimeOffset object and a specified object represent a single point in time. It directly compares neither dates and times nor offsets. To determine whether two DateTimeOffset objects represent the same time and have the same offset value, use the EqualsExact method.

If obj is null, or if the run-time type of obj is not DateTimeOffset, the method returns false.

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(DateTimeOffset, DateTimeOffset)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

Determines whether two specified DateTimeOffset objects represent the same point in time.

C#
public static bool Equals(DateTimeOffset first, DateTimeOffset second);

Parameters

first
DateTimeOffset

The first object to compare.

second
DateTimeOffset

The second object to compare.

Returns

true if the two DateTimeOffset objects have the same UtcDateTime value; otherwise, false.

Examples

The following example illustrates calls to the Equals(DateTimeOffset, DateTimeOffset) method to test various pairs of DateTimeOffset objects for equality.

C#
DateTimeOffset firstTime = new DateTimeOffset(2007, 11, 15, 11, 35, 00,
                                    DateTimeOffset.Now.Offset);
DateTimeOffset secondTime = firstTime;
Console.WriteLine("{0} = {1}: {2}",
                  firstTime, secondTime,
                  DateTimeOffset.Equals(firstTime, secondTime));

// The value of firstTime remains unchanged
secondTime = new DateTimeOffset(firstTime.DateTime,
             TimeSpan.FromHours(firstTime.Offset.Hours + 1));
Console.WriteLine("{0} = {1}: {2}",
                  firstTime, secondTime,
                  DateTimeOffset.Equals(firstTime, secondTime));

// value of firstTime remains unchanged
secondTime = new DateTimeOffset(firstTime.DateTime + TimeSpan.FromHours(1),
                                TimeSpan.FromHours(firstTime.Offset.Hours + 1));
Console.WriteLine("{0} = {1}: {2}",
                  firstTime, secondTime,
                  DateTimeOffset.Equals(firstTime, secondTime));
 // The example produces the following output:
 //       11/15/2007 11:35:00 AM -07:00 = 11/15/2007 11:35:00 AM -07:00: True
 //       11/15/2007 11:35:00 AM -07:00 = 11/15/2007 11:35:00 AM -06:00: False
 //       11/15/2007 11:35:00 AM -07:00 = 11/15/2007 12:35:00 PM -06:00: True

Remarks

Before it performs the comparison, this method converts both DateTimeOffset objects to Coordinated Universal Time (UTC). The method is equivalent to the following:

C#
return first.UtcDateTime == second.UtcDateTime;

In other words, the Equals(DateTimeOffset, DateTimeOffset) method determines whether the two DateTimeOffset objects represent a single point in time. It directly compares neither dates and times nor offsets. To determine whether two DateTimeOffset objects represent the same time and have the same offset value, use the EqualsExact method.

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