共用方式為


DateTimeOffset.Equals 方法

定義

判斷兩個 DateTimeOffset 物件是相等,還是 DateTimeOffset 物件等於指定的物件。

多載

Equals(DateTimeOffset)

判斷目前 DateTimeOffset 物件與指定的 DateTimeOffset 物件是否代表相同的時間點。

Equals(Object)

判斷 DateTimeOffset 物件與指定的物件是否代表相同的時間點。

Equals(DateTimeOffset, DateTimeOffset)

判斷兩個指定的 DateTimeOffset 物件是否代表相同的時間點。

Equals(DateTimeOffset)

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
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)

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
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)

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
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 方法。

另請參閱

適用於