DateTime.Equality(DateTime, DateTime) 運算子

定義

判斷 DateTime 兩個指定的執行個體是否相同。Determines whether two specified instances of DateTime are equal.

public:
 static bool operator ==(DateTime d1, DateTime d2);
public static bool operator == (DateTime d1, DateTime d2);
static member ( = ) : DateTime * DateTime -> bool
Public Shared Operator == (d1 As DateTime, d2 As DateTime) As Boolean

參數

d1
DateTime

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

d2
DateTime

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

傳回

trued1 代表相同的日期和時間時為 d2,否則為 falsetrue if d1 and d2 represent the same date and time; otherwise, false.

範例

下列範例示範等號比較運算子。The following example demonstrates the equality operator.

System::DateTime april19( 2001, 4, 19 );
System::DateTime otherDate( 1991, 6, 5 );

// areEqual gets false.
bool areEqual = april19 == otherDate;

otherDate = DateTime( 2001, 4, 19 );
// areEqual gets true.
areEqual = april19 == otherDate;
System.DateTime april19 = new DateTime(2001, 4, 19);
System.DateTime otherDate = new DateTime(1991, 6, 5);

// areEqual gets false.
bool areEqual = april19 == otherDate;
    
otherDate = new DateTime(2001, 4, 19);
// areEqual gets true.
areEqual = april19 == otherDate;
Dim april19 As New DateTime(2001, 4, 19)
Dim otherDate As New DateTime(1991, 6, 5)

Dim areEqual As Boolean
' areEqual gets false.
areEqual = DateTime.op_Equality(april19, otherDate)

otherDate = New DateTime(2001, 4, 19)
' areEqual gets true.
areEqual = System.DateTime.op_Equality(april19, otherDate)

備註

Equality 運算子會藉由比較其刻度數,判斷兩個 DateTime 值是否相等。The Equality operator determines whether two DateTime values are equal by comparing their number of ticks. 在比較 DateTime 物件之前,請確定物件代表相同時區中的時間。Before comparing DateTime objects, make sure that the objects represent times in the same time zone. 只要比較其 Kind 屬性的值,即可做到這一點。You can do this by comparing the values of their Kind property.

這個運算子的對等方法是 DateTime.Equals(Object)The equivalent method for this operator is DateTime.Equals(Object)

適用於

另請參閱