Bagikan melalui


DateTime.Equals Metode

Definisi

Mengembalikan nilai yang menunjukkan apakah dua DateTime objek, atau DateTime instans dan objek lain atau DateTime, memiliki nilai yang sama.

Overload

Nama Deskripsi
Equals(DateTime)

Mengembalikan nilai yang menunjukkan apakah nilai instans ini sama dengan nilai instans yang ditentukan DateTime .

Equals(Object)

Mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu.

Equals(DateTime, DateTime)

Mengembalikan nilai yang menunjukkan apakah dua DateTime instans memiliki nilai tanggal dan waktu yang sama.

Equals(DateTime)

Sumber:
DateTime.cs
Sumber:
DateTime.cs
Sumber:
DateTime.cs
Sumber:
DateTime.cs
Sumber:
DateTime.cs

Mengembalikan nilai yang menunjukkan apakah nilai instans ini sama dengan nilai instans yang ditentukan 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

Parameter

value
DateTime

Objek untuk dibandingkan dengan instans ini.

Mengembalikan

true value jika parameter sama dengan nilai instans ini; jika tidak, false.

Penerapan

Contoh

Contoh berikut menunjukkan Equals metode .

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.

Keterangan

Instans saat ini dan value sama jika nilai propertinya Ticks sama. Nilai properti mereka Kind tidak dipertimbangkan dalam pengujian untuk kesetaraan.

Metode ini mengimplementasikan System.IEquatable<T> antarmuka, dan berkinerja sedikit lebih baik daripada Equals metode karena value parameter tidak harus dikonversi ke objek.

Lihat juga

Berlaku untuk

Equals(Object)

Sumber:
DateTime.cs
Sumber:
DateTime.cs
Sumber:
DateTime.cs
Sumber:
DateTime.cs
Sumber:
DateTime.cs

Mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu.

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

Parameter

value
Object

Objek untuk dibandingkan dengan instans ini.

Mengembalikan

true jika value adalah instans DateTime dan sama dengan nilai instans ini; jika tidak, false.

Contoh

Contoh berikut menunjukkan Equals metode .

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.

Keterangan

Instans saat ini dan value sama jika nilai propertinya Ticks sama. Nilai properti mereka Kind tidak dipertimbangkan dalam pengujian untuk kesetaraan.

Lihat juga

Berlaku untuk

Equals(DateTime, DateTime)

Sumber:
DateTime.cs
Sumber:
DateTime.cs
Sumber:
DateTime.cs
Sumber:
DateTime.cs
Sumber:
DateTime.cs

Mengembalikan nilai yang menunjukkan apakah dua DateTime instans memiliki nilai tanggal dan waktu yang sama.

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

Parameter

t1
DateTime

Objek pertama yang dibandingkan.

t2
DateTime

Objek kedua yang akan dibandingkan.

Mengembalikan

true jika kedua nilai sama; jika tidak, false.

Contoh

Contoh berikut menunjukkan Equals metode .

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)

Keterangan

t1 dan t2 sama jika nilai propertinya Ticks sama. Nilai properti mereka Kind tidak dipertimbangkan dalam pengujian untuk kesetaraan.

Lihat juga

Berlaku untuk