共用方式為


DateTime.Equals 方法

定義

回傳一個值,表示兩個 DateTime 物件,或一個 DateTime 實例與另一個物件 或 DateTime,是否有相同的值。

多載

名稱 Description
Equals(DateTime)

回傳一個值,表示該實例的值是否等於指定 DateTime 實例的值。

Equals(Object)

傳回值,指出這個實例是否等於指定的物件。

Equals(DateTime, DateTime)

回傳一個值,表示兩個 DateTime 實例的日期與時間值是否相同。

Equals(DateTime)

來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs

回傳一個值,表示該實例的值是否等於指定 DateTime 實例的值。

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

參數

value
DateTime

這個例子是要比較的對象。

傳回

true 若參數 value 等於該實例的值;否則, false

實作

範例

以下範例示範此 Equals 方法。

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.
open System

// Create some DateTime objects.
let one = DateTime.UtcNow

let two = DateTime.Now

let three = one

// Compare the DateTime objects and display the results.
let result = one.Equals two

printfn $"The result of comparing DateTime object one and two is: {result}."

let result2 = one.Equals three

printfn $"The result of comparing DateTime object one and three is: {result2}."

// 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.
Module Application

    Sub Main()
        ' Create some DateTime objects.
        Dim one As DateTime = DateTime.UtcNow

        Dim two As DateTime = DateTime.Now

        Dim three As DateTime = one

        ' Compare the DateTime objects and display the results.
        Dim result As Boolean = 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)

    End Sub
End Module

' 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.

備註

當前實例 和 value 若屬性值相等,則 Ticks 為相等。 他們的 Kind 財產價值不被納入平等測試。

此方法實作了介面, System.IEquatable<T> 且表現略優於該 Equals 方法,因為 value 參數不必轉換為物件。

另請參閱

適用於

Equals(Object)

來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs

傳回值,指出這個實例是否等於指定的物件。

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

參數

value
Object

這個例子是要比較的對象。

傳回

truevalue 是 的 DateTime 一個實例,且 等於該實例的值;否則, false

範例

以下範例示範此 Equals 方法。

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.
open System

// Create some DateTime objects.
let one = DateTime.UtcNow

let two = DateTime.Now

let three = one

// Compare the DateTime objects and display the results.
let result = one.Equals two

printfn $"The result of comparing DateTime object one and two is: {result}."

let result2 = one.Equals three

printfn $"The result of comparing DateTime object one and three is: {result2}."

// 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.
Module Application

    Sub Main()
        ' Create some DateTime objects.
        Dim one As DateTime = DateTime.UtcNow

        Dim two As DateTime = DateTime.Now

        Dim three As DateTime = one

        ' Compare the DateTime objects and display the results.
        Dim result As Boolean = 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)

    End Sub
End Module

' 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.

備註

當前實例 和 value 若屬性值相等,則 Ticks 為相等。 他們的 Kind 財產價值不被納入平等測試。

另請參閱

適用於

Equals(DateTime, DateTime)

來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs

回傳一個值,表示兩個 DateTime 實例的日期與時間值是否相同。

public:
 static bool Equals(DateTime t1, DateTime t2);
public static bool Equals(DateTime t1, DateTime t2);
static member Equals : DateTime * DateTime -> bool
Public Shared Function Equals (t1 As DateTime, t2 As DateTime) As Boolean

參數

t1
DateTime

第一個比較的對象。

t2
DateTime

第二個比較物件。

傳回

true若兩個值相等;否則,。 false

範例

以下範例示範此 Equals 方法。

let today1 = 
    System.DateTime System.DateTime.Today.Ticks

let today2 =
    System.DateTime System.DateTime.Today.Ticks

let tomorrow =
    System.DateTime.Today.AddDays(1).Ticks
    |> System.DateTime 

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

// todayEqualsTomorrow gets false.
let todayEqualsTomorrow = System.DateTime.Equals(today1, tomorrow)
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);
Dim today1 As New System.DateTime(System.DateTime.Today.Ticks)
Dim today2 As New System.DateTime(System.DateTime.Today.Ticks)
Dim tomorrow As New System.DateTime( _
                        System.DateTime.Today.AddDays(1).Ticks)

' todayEqualsToday gets true.
Dim todayEqualsToday As Boolean = System.DateTime.Equals(today1, today2)

' todayEqualsTomorrow gets false.
Dim todayEqualsTomorrow As Boolean = System.DateTime.Equals(today1, tomorrow)

備註

t1 若 與 t2 的性質值相等,則 Ticks 相等。 他們的 Kind 財產價值不被納入平等測試。

另請參閱

適用於