DateTime.Equals Metoda

Definicja

Zwraca wartość wskazującą, czy dwa DateTime obiekty, DateTime czy wystąpienie i inny obiekt lub DateTime, mają tę samą wartość.

Przeciążenia

Equals(DateTime)

Zwraca wartość wskazującą, czy wartość tego wystąpienia jest równa wartości określonego DateTime wystąpienia.

Equals(Object)

Zwraca wartość wskazującą, czy to wystąpienie jest równe podanemu obiektowi.

Equals(DateTime, DateTime)

Zwraca wartość wskazującą, czy dwa DateTime wystąpienia mają tę samą wartość daty i godziny.

Equals(DateTime)

Zwraca wartość wskazującą, czy wartość tego wystąpienia jest równa wartości określonego DateTime wystąpienia.

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

Parametry

value
DateTime

Obiekt do porównania z tym wystąpieniem.

Zwraca

Boolean

truevalue jeśli parametr jest równy wartości tego wystąpienia; w przeciwnym razie . false

Implementuje

Przykłady

W poniższym przykładzie pokazano metodę 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.

Uwagi

Bieżące wystąpienie i value są równe, jeśli ich Ticks wartości właściwości są równe. Ich Kind wartości właściwości nie są brane pod uwagę w teście równości.

Ta metoda implementuje System.IEquatable<T> interfejs i działa nieco lepiej niż Equals metoda, ponieważ value parametr nie musi być konwertowany na obiekt.

Zobacz też

Dotyczy

Equals(Object)

Zwraca wartość wskazującą, czy to wystąpienie jest równe podanemu obiektowi.

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

Parametry

value
Object

Obiekt do porównania z tym wystąpieniem.

Zwraca

Boolean

truejeśli value jest wystąpieniem DateTime i równa wartości tego wystąpienia; w przeciwnym razie . false

Przykłady

W poniższym przykładzie pokazano metodę 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.

Uwagi

Bieżące wystąpienie i value są równe, jeśli ich Ticks wartości właściwości są równe. Ich Kind wartości właściwości nie są brane pod uwagę w teście równości.

Zobacz też

Dotyczy

Equals(DateTime, DateTime)

Zwraca wartość wskazującą, czy dwa DateTime wystąpienia mają tę samą wartość daty i godziny.

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

Parametry

t1
DateTime

Pierwszy obiekt do porównania.

t2
DateTime

Drugi obiekt do porównania.

Zwraca

Boolean

true jeśli dwie wartości są równe; w przeciwnym razie , false.

Przykłady

W poniższym przykładzie pokazano metodę Equals .

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 );
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)

Uwagi

t1 i t2 są równe, jeśli ich Ticks wartości właściwości są równe. Ich Kind wartości właściwości nie są brane pod uwagę w teście równości.

Zobacz też

Dotyczy