通过


DateTime.Equals 方法

定义

返回一个值,该值指示两个对象,还是一 DateTimeDateTime 实例和另一个对象,或者 DateTime具有相同的值。

重载

名称 说明
Equals(DateTime)

返回一个值,该值指示此实例的值是否等于指定 DateTime 实例的值。

Equals(Object)

返回一个值,该值指示此实例是否等于指定的对象。

Equals(DateTime, DateTime)

返回一个值,该值指示两 DateTime 个实例是否具有相同的日期和时间值。

Equals(DateTime)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

返回一个值,该值指示此实例的值是否等于指定 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

参数

value
DateTime

要与此实例进行比较的对象。

返回

true value如果参数等于此实例的值,则为 ;否则为 false

实现

示例

以下示例演示了该方法 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.

注解

当前实例,如果它们的属性值相等,则 valueTicks 等。 在测试中不考虑它们的 Kind 属性值是否相等。

此方法实现 System.IEquatable<T> 接口,并且性能略高于方法, Equals 因为 value 参数不必转换为对象。

另请参阅

适用于

Equals(Object)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

返回一个值,该值指示此实例是否等于指定的对象。

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

参数

value
Object

要与此实例进行比较的对象。

返回

true 如果 value 为此实例的 DateTime 实例且等于此实例的值,则为 ;否则为 false

示例

以下示例演示了该方法 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.

注解

当前实例,如果它们的属性值相等,则 valueTicks 等。 在测试中不考虑它们的 Kind 属性值是否相等。

另请参阅

适用于

Equals(DateTime, DateTime)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

返回一个值,该值指示两 DateTime 个实例是否具有相同的日期和时间值。

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

要比较的第一个对象。

t2
DateTime

要比较的第二个对象。

返回

true 如果两个值相等,则为否则,为 false.

示例

以下示例演示了该方法 Equals

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)

注解

t1如果属性值Ticks相等,则t2为相等。 在测试中不考虑它们的 Kind 属性值是否相等。

另请参阅

适用于