DateTime.Equals Metod

Definition

Returnerar ett värde som anger om två DateTime objekt, eller en instans och ett DateTime annat objekt eller DateTime, har samma värde.

Överlagringar

Name Description
Equals(DateTime)

Returnerar ett värde som anger om värdet för den här instansen är lika med värdet för den angivna DateTime instansen.

Equals(Object)

Returnerar ett värde som anger om den här instansen är lika med ett angivet objekt.

Equals(DateTime, DateTime)

Returnerar ett värde som anger om två DateTime instanser har samma datum- och tidsvärde.

Equals(DateTime)

Källa:
DateTime.cs
Källa:
DateTime.cs
Källa:
DateTime.cs
Källa:
DateTime.cs
Källa:
DateTime.cs

Returnerar ett värde som anger om värdet för den här instansen är lika med värdet för den angivna DateTime instansen.

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

Parametrar

value
DateTime

Objektet som ska jämföras med den här instansen.

Returer

trueom parametern value är lika med värdet för den här instansen, annars . false

Implementeringar

Exempel

I följande exempel visas Equals metoden.

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.

Kommentarer

Den aktuella instansen och value är lika om deras Ticks egenskapsvärden är lika. Deras Kind egenskapsvärden beaktas inte i testet för likhet.

Den här metoden implementerar System.IEquatable<T> gränssnittet och fungerar något bättre än Equals metoden eftersom parametern value inte behöver konverteras till ett objekt.

Se även

Gäller för

Equals(Object)

Källa:
DateTime.cs
Källa:
DateTime.cs
Källa:
DateTime.cs
Källa:
DateTime.cs
Källa:
DateTime.cs

Returnerar ett värde som anger om den här instansen är lika med ett angivet objekt.

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

Parametrar

value
Object

Objektet som ska jämföras med den här instansen.

Returer

trueom value är en instans av DateTime och är lika med värdet för den här instansen, annars . false

Exempel

I följande exempel visas Equals metoden.

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.

Kommentarer

Den aktuella instansen och value är lika om deras Ticks egenskapsvärden är lika. Deras Kind egenskapsvärden beaktas inte i testet för likhet.

Se även

Gäller för

Equals(DateTime, DateTime)

Källa:
DateTime.cs
Källa:
DateTime.cs
Källa:
DateTime.cs
Källa:
DateTime.cs
Källa:
DateTime.cs

Returnerar ett värde som anger om två DateTime instanser har samma datum- och tidsvärde.

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

Parametrar

t1
DateTime

Det första objektet som ska jämföras.

t2
DateTime

Det andra objektet att jämföra.

Returer

trueom de två värdena är lika med; annars . false

Exempel

I följande exempel visas Equals metoden.

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)

Kommentarer

t1 och t2 är lika om deras Ticks egenskapsvärden är lika. Deras Kind egenskapsvärden beaktas inte i testet för likhet.

Se även

Gäller för