Redigeeri

Jagamisviis:


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.

public:
 virtual bool Equals(DateTimeOffset other);
public bool Equals (DateTimeOffset other);
override this.Equals : DateTimeOffset -> bool
Public Function Equals (other As DateTimeOffset) As Boolean

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.

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
let firstTime = DateTimeOffset(2007, 9, 1, 6, 45, 0, TimeSpan(-7, 0, 0))

let secondTime = firstTime
printfn $"{firstTime} = {secondTime}: {firstTime.Equals secondTime}"

let secondTime = DateTimeOffset(2007, 9, 1, 6, 45, 0, TimeSpan(-6, 0, 0))
printfn $"{firstTime} = {secondTime}: {firstTime.Equals secondTime}"

let secondTime = DateTimeOffset(2007, 9, 1, 8, 45, 0, TimeSpan(-5, 0, 0))
printfn $"{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
    Dim firstTime As New DateTimeOffset(#09/01/2007 6:45:00AM#, _
                     New TimeSpan(-7, 0, 0))

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

    secondTime = New DateTimeOffset(#09/01/2007 6:45:00AM#, _
                     New TimeSpan(-6, 0, 0))      
    Console.WriteLine("{0} = {1}: {2}", _
                     firstTime, secondTime, _
                     firstTime.Equals(secondTime))
    
    secondTime = New DateTimeOffset(#09/01/2007 8:45:00AM#, _
                     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:

return this.UtcDateTime == other.UtcDateTime;
this.UtcDateTime = other.UtcDateTime
Return Me.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

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.

public:
 override bool Equals(System::Object ^ obj);
public override bool Equals (object obj);
public override bool Equals (object? obj);
override this.Equals : obj -> bool
Public Overrides Function Equals (obj As Object) As Boolean

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.

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
let compareForEquality2 () =
    let firstTime = DateTimeOffset(2007, 9, 1, 6, 45, 0, TimeSpan(-7, 0, 0))

    let secondTime: obj = firstTime
    printfn $"{firstTime} = {secondTime}: {firstTime.Equals secondTime}"

    let secondTime = DateTimeOffset(2007, 9, 1, 6, 45, 0, TimeSpan(-6, 0, 0))
    printfn $"{firstTime} = {secondTime}: {firstTime.Equals secondTime}"

    let secondTime = DateTimeOffset(2007, 9, 1, 8, 45, 0, TimeSpan(-5, 0, 0))
    printfn $"{firstTime} = {secondTime}: {firstTime.Equals secondTime}"

    let secondTime = null
    printfn $"{firstTime} = {secondTime}: {firstTime.Equals secondTime}"

    let secondTime = DateTime(2007, 9, 1, 6, 45, 00)
    printfn $"{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
    Dim firstTime As New DateTimeOffset(#09/01/2007 6:45:00AM#, _
                     New TimeSpan(-7, 0, 0))

    Dim secondTime As Object = firstTime
    Console.WriteLine("{0} = {1}: {2}", _
                      firstTime, secondTime, _
                      firstTime.Equals(secondTime))

    secondTime = New DateTimeOffset(#09/01/2007 6:45:00AM#, _
                     New TimeSpan(-6, 0, 0))      
    Console.WriteLine("{0} = {1}: {2}", _
                     firstTime, secondTime, _
                     firstTime.Equals(secondTime))
    
    secondTime = New DateTimeOffset(#09/01/2007 8:45:00AM#, _
                     New TimeSpan(-5, 0, 0))
    Console.WriteLine("{0} = {1}: {2}", _
                     firstTime, secondTime, _
                     firstTime.Equals(secondTime))
                     
    secondTime = Nothing
    Console.WriteLine("{0} = {1}: {2}", _
                     firstTime, secondTime, _
                     firstTime.Equals(secondTime))

    secondTime = #9/1/2007 6:45AM#
    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:

return this.UtcDateTime == ((DateTimeOffset) obj).UtcDateTime;
this.UtcDateTime = (obj :?> DateTimeOffset).UtcDateTime
Return Me.UtcDateTime = DirectCast(obj, DatetimeOffset).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

Equals(DateTimeOffset, DateTimeOffset)

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

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

public:
 static bool Equals(DateTimeOffset first, DateTimeOffset second);
public static bool Equals (DateTimeOffset first, DateTimeOffset second);
static member Equals : DateTimeOffset * DateTimeOffset -> bool
Public Shared Function Equals (first As DateTimeOffset, second As DateTimeOffset) As Boolean

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.

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
let firstTime = DateTimeOffset(2007, 11, 15, 11, 35, 00, DateTimeOffset.Now.Offset)
let secondTime = firstTime
printfn $"{firstTime} = {secondTime}: {DateTimeOffset.Equals(firstTime, secondTime)}"

// The value of firstTime remains unchanged
let secondTime = DateTimeOffset(firstTime.DateTime, TimeSpan.FromHours(firstTime.Offset.Hours + 1 |> float))
printfn $"{firstTime} = {secondTime}: {DateTimeOffset.Equals(firstTime, secondTime)}"

// value of firstTime remains unchanged
let secondTime = DateTimeOffset(firstTime.DateTime + TimeSpan.FromHours 1, TimeSpan.FromHours(firstTime.Offset.Hours + 1 |> float))
printfn $"{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
Dim firstTime As New DateTimeOffset(#11/15/2007 11:35AM#, _
                                    DateTimeOffset.Now.Offset)
Dim secondTime As DateTimeOffset = 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:

return first.UtcDateTime == second.UtcDateTime;
first.UtcDateTime = second.UtcDateTime
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