DateTime.Equals 方法

定義

傳回值,指出兩個 DateTime 物件,或 DateTime 執行個體和另一個物件或 DateTime 是否有相同的值。Returns a value indicating whether two DateTime objects, or a DateTime instance and another object or DateTime, have the same value.

多載

Equals(DateTime, DateTime)

傳回值,指出兩個 DateTime 執行個體是否有相同的日期和時間值。Returns a value indicating whether two DateTime instances have the same date and time value.

Equals(DateTime)

傳回值,該值指出這個執行個體的值是否等於指定之 DateTime 執行個體的值。Returns a value indicating whether the value of this instance is equal to the value of the specified DateTime instance.

Equals(Object)

傳回值,指出此執行個體是否等於指定的物件。Returns a value indicating whether this instance is equal to a specified object.

Equals(DateTime, DateTime)

傳回值,指出兩個 DateTime 執行個體是否有相同的日期和時間值。Returns a value indicating whether two DateTime instances have the same date and time value.

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

要比較的第一個物件。The first object to compare.

t2
DateTime

要比較的第二個物件。The second object to compare.

傳回

當兩個值相等時為 true,否則為 falsetrue if the two values are equal; otherwise, false.

範例

下列範例示範 Equals 方法。The following example demonstrates the Equals method.

System::DateTime today1 = System::DateTime(
   System::DateTime::Today.Ticks );
System::DateTime today2 = System::DateTime(
   System::DateTime::Today.Ticks );
System::DateTime tomorrow = 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 );
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)

備註

如果 t1t2Ticks 屬性值相等,則相等。t1 and t2 are equal if their Ticks property values are equal. 在測試中,不會將其 Kind 的屬性值視為相等。Their Kind property values are not considered in the test for equality.

另請參閱

Equals(DateTime)

傳回值,該值指出這個執行個體的值是否等於指定之 DateTime 執行個體的值。Returns a value indicating whether the value of this instance is equal to the value of the specified DateTime instance.

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

與這個執行個體相比較的物件。The object to compare to this instance.

傳回

true 參數等於這個執行個體的值時為 value,否則為 falsetrue if the value parameter equals the value of this instance; otherwise, false.

實作

範例

下列範例示範 Equals 方法。The following example demonstrates the Equals method.

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

備註

如果目前的實例和 valueTicks 屬性值相等,則其為相等。The current instance and value are equal if their Ticks property values are equal. 在測試中,不會將其 Kind 的屬性值視為相等。Their Kind property values are not considered in the test for equality.

這個方法會實作用 System.IEquatable<T> 介面,而且執行效果稍微優於 Equals 方法,因為 value 參數不需要轉換成物件。This method implements the System.IEquatable<T> interface, and performs slightly better than the Equals method because the value parameter does not have to be converted to an object.

另請參閱

Equals(Object)

傳回值,指出此執行個體是否等於指定的物件。Returns a value indicating whether this instance is equal to a specified object.

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

參數

value
Object

與這個執行個體相比較的物件。The object to compare to this instance.

傳回

如果 truevalue 的執行個體,並且等於這個執行個體的值,則為 DateTime,否則為 falsetrue if value is an instance of DateTime and equals the value of this instance; otherwise, false.

範例

下列範例示範 Equals 方法。The following example demonstrates the Equals method.

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

備註

如果目前的實例和 valueTicks 屬性值相等,則其為相等。The current instance and value are equal if their Ticks property values are equal. 在測試中,不會將其 Kind 的屬性值視為相等。Their Kind property values are not considered in the test for equality.

另請參閱

適用於