DateTimeOffset.Equals 方法

定义

确定两个 DateTimeOffset 对象是否相等,或者 DateTimeOffset 对象是否与指定的对象相等。

重载

Equals(DateTimeOffset)

确定当前的 DateTimeOffset 对象是否与指定的 DateTimeOffset 对象表示同一时间点。

Equals(Object)

确定 DateTimeOffset 对象是否与指定的对象表示同一时间点。

Equals(DateTimeOffset, DateTimeOffset)

确定两个指定的 DateTimeOffset 对象是否表示同一时间点。

Equals(DateTimeOffset)

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

确定当前的 DateTimeOffset 对象是否与指定的 DateTimeOffset 对象表示同一时间点。

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

参数

other
DateTimeOffset

要与当前 DateTimeOffset 对象进行比较的对象。

返回

如果两个 DateTimeOffset 对象具有相同的 UtcDateTime 值,则为 true;否则为 false

实现

示例

下面的示例演示了对 方法的调用, Equals(DateTimeOffset) 以测试 DateTimeOffset 对象是否与当前 DateTimeOffset 对象相等。

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

注解

在执行比较之前,此方法将这两个 DateTimeOffset 对象的值转换为协调世界时 (UTC) 。 方法等效于以下内容:

return this.UtcDateTime == other.UtcDateTime;
this.UtcDateTime = other.UtcDateTime
Return Me.UtcDateTime = other.UtcDateTime

换句话说, Equals(DateTimeOffset) 方法确定两个 DateTimeOffset 对象是否表示单个时间点。 它不直接比较日期和时间,也不直接比较偏移量。 若要确定两个 DateTimeOffset 对象是否表示同一时间并具有相同的偏移值,请使用 EqualsExact 方法。

DateTimeOffsetnull为 的对象被视为晚于 (或) 为 null的对象。

方法的 Equals(DateTimeOffset) 此重载实现 IEquatable<T>.Equals 方法。 它提供的性能略高于 DateTimeOffset.Equals(Object) 重载, other 因为参数不必从 对象转换。

另请参阅

适用于

Equals(Object)

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

确定 DateTimeOffset 对象是否与指定的对象表示同一时间点。

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

参数

obj
Object

要与当前 DateTimeOffset 对象进行比较的对象。

返回

如果 obj 参数是 DateTimeOffset 对象,并且与当前的 DateTimeOffset 对象表示同一时间点,则为 true;否则为 false

示例

以下示例指示当前 DateTimeOffset 对象是否等于其他 DateTimeOffset 几个对象,以及 null 引用和 DateTime 对象。

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

注解

在执行比较之前,此方法将当前 DateTimeOffset 对象和 obj 参数的值转换为协调世界时 (UTC) 。 方法等效于以下内容:

return this.UtcDateTime == ((DateTimeOffset) obj).UtcDateTime;
this.UtcDateTime = (obj :?> DateTimeOffset).UtcDateTime
Return Me.UtcDateTime = DirectCast(obj, DatetimeOffset).UtcDateTime

换句话说, DateTimeOffset.Equals(Object) 方法确定当前 DateTimeOffset 对象和指定的 对象是否表示单个时间点。 它不直接比较日期和时间,也不直接比较偏移量。 若要确定两个 DateTimeOffset 对象是否表示同一时间并具有相同的偏移值,请使用 EqualsExact 方法。

如果 objnull,或者 如果 的 obj 运行时类型不是 DateTimeOffset,则方法返回 false

另请参阅

适用于

Equals(DateTimeOffset, DateTimeOffset)

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

确定两个指定的 DateTimeOffset 对象是否表示同一时间点。

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

参数

first
DateTimeOffset

要比较的第一个对象。

second
DateTimeOffset

要比较的第二个对象。

返回

如果这两个 DateTimeOffset 对象具有相同的 UtcDateTime 值,则为 true;否则为 false

示例

下面的示例演示了对 方法的调用, Equals(DateTimeOffset, DateTimeOffset) 以测试各种对象对 DateTimeOffset 是否相等。

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

注解

在执行比较之前,此方法将这两个 DateTimeOffset 对象转换为协调世界时 (UTC) 。 方法等效于以下内容:

return first.UtcDateTime == second.UtcDateTime;
first.UtcDateTime = second.UtcDateTime
Return first.UtcDateTime = second.UtcDateTime

换句话说, Equals(DateTimeOffset, DateTimeOffset) 方法确定这两 DateTimeOffset 个 对象是否表示单个时间点。 它不直接比较日期和时间,也不直接比较偏移量。 若要确定两个 DateTimeOffset 对象是否表示同一时间并具有相同的偏移值,请使用 EqualsExact 方法。

另请参阅

适用于