📜  C#中的DateTime.Equals()方法

📅  最后修改于: 2021-05-30 00:33:17             🧑  作者: Mango

此方法用于获取一个值,该值指示两个DateTime对象或一个DateTime实例与另一个对象或DateTime是否具有相同的值。此方法的重载列表中共有3种方法:

  • 等于(DateTime,DateTime)
  • 等于(DateTime)
  • 等于(对象)
等于(DateTime,DateTime)

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

句法:

public static bool Equals (DateTime t1, DateTime t2);

参数:

  • t1:要比较的第一个对象。
  • t2:要比较的第二个对象。

返回值:如果两个值相等,则此方法返回true;否则,返回true。否则为假。

下面的程序说明了DateTime.Equals(DateTime,DateTime)方法的用法:

范例1:

// C# program to demonstrate the
// DateTime.Equals(DateTime,
//  DateTime) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating object of DateTime
        DateTime date1 = new DateTime(2010, 1,
                                 1, 4, 0, 15);
  
        // creating object of DateTime
        DateTime date2 = new DateTime(2010, 1,
                                 1, 4, 0, 14);
  
        // comparing date1 and date2
        // using Equals() method;
        bool value = DateTime.Equals(date1, date2);
  
        // checking
        if (value)
            Console.Write("date1 is rquals to date2. ");
        else
            Console.Write("date1 is not equals to date2. ");
    }
}

输出:

date1 is not equals to date2.

范例2:

// C# program to demonstrate the
// DateTime.Equals(DateTime,
// DateTime) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // calling check() method
        check(new DateTime(2010, 1, 3, 4, 0, 15),
              new DateTime(2010, 1, 4, 4, 0, 15));
  
        check(new DateTime(2010, 1, 5, 4, 0, 15),
              new DateTime(2010, 1, 4, 4, 0, 15));
  
        check(new DateTime(2010, 1, 5, 4, 0, 15),
              new DateTime(2010, 1, 5, 4, 0, 15));
    }
    public static void check(DateTime date1, DateTime date2)
    {
  
        // comparing date1 and date2
        // using Equals() method;
        bool value = DateTime.Equals(date1, date2);
  
        // checking
        if (value)
            Console.WriteLine(" {0:d} is equals to"+
                          " {1:d}. ", date1, date2);
        else
            Console.WriteLine(" {0:d} is not equals"+
                        " to {1:d}. ", date1, date2);
    }
}

输出:

1/3/2010 is not equals to 1/4/2010. 
1/5/2010 is not equals to 1/4/2010. 
1/5/2010 is equals to 1/5/2010. 
等于(DateTime)

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

句法:

public bool Equals (DateTime value);

在这里,需要对象与该实例进行比较。

返回值:如果value参数等于此实例的值,则此方法返回true;否则,此方法返回true。否则为假。

下面的程序说明了DateTime.Equals(DateTime)方法的用法:

范例1:

// C# program to demonstrate the
// DateTime.Equals(DateTime) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating object of DateTime
        DateTime date1 = new DateTime(2010, 1,
                                 1, 4, 0, 15);
  
        // creating object of DateTime
        DateTime date2 = new DateTime(2010, 1,
                                 1, 4, 0, 14);
  
        // comparing date1 and date2
        // using Equals() method;
        bool value = date1.Equals(date2);
  
        // checking
        if (value)
            Console.Write("date1 is rquals to date2. ");
        else
            Console.Write("date1 is not equals to date2. ");
    }
}

输出:

date1 is not equals to date2. 

范例2:

// C# program to demonstrate the
// DateTime.Equals(DateTime) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // calling check() method
        check(new DateTime(2010, 1, 3, 4, 0, 15),
              new DateTime(2010, 1, 4, 4, 0, 15));
  
        check(new DateTime(2010, 1, 5, 4, 0, 15),
              new DateTime(2010, 1, 4, 4, 0, 15));
  
        check(new DateTime(2010, 1, 5, 4, 0, 15),
              new DateTime(2010, 1, 5, 4, 0, 15));
    }
  
    public static void check(DateTime date1,
                             DateTime date2)
    {
  
        // comparing date1 and date2
        // using Equals() method;
        bool value = date1.Equals(date2);
  
        // checking
        if (value)
            Console.WriteLine(" {0:d} is equals to"+
                          " {1:d}. ", date1, date2);
        else
            Console.WriteLine(" {0:d} is not equals "+
                          "to {1:d}. ", date1, date2);
    }
}

输出:

01/03/2010 is not equals to 01/04/2010. 
01/05/2010 is not equals to 01/04/2010. 
01/05/2010 is equals to 01/05/2010. 
等于(对象)

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

句法:

public override bool Equals (object value);

在这里,需要对象与该实例进行比较。

返回值:如果value参数等于此实例的值,则此方法返回true,否则返回false。

下面的程序说明了DateTime.Equals(Object)方法的用法:

范例1:

// C# program to demonstrate the
// DateTime.Equals(DateTime) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating object of DateTime
        DateTime date1 = new DateTime(2010, 1,
                                  1, 4, 0, 15);
  
        // creating object of DateTime
        DateTime date2 = new DateTime(2010, 1,
                                 1, 4, 0, 14);
  
        // comparing date1 and date2
        // using Equals() method;
        bool value = date1.Equals(date2);
  
        // checking
        if (value)
            Console.Write("date1 is rquals to date2. ");
        else
            Console.Write("date1 is not equals to date2. ");
    }
}

输出:

date1 is not equals to date2. 

范例2:

// C# program to demonstrate the
// DateTime.Equals(DateTime) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // calling check() method
        check(new DateTime(2010, 1, 3, 4, 0, 15),
              new DateTime(2010, 1, 4, 4, 0, 15));
  
        check(new DateTime(2010, 1, 5, 4, 0, 15),
              new DateTime(2010, 1, 4, 4, 0, 15));
  
        check(new DateTime(2010, 1, 5, 4, 0, 15),
              new DateTime(2010, 1, 5, 4, 0, 15));
    }
  
    public static void check(DateTime date1,
                             DateTime date2)
    {
  
        // comparing date1 and date2
        // using Equals() method;
        bool value = date1.Equals(date2);
  
        // checking
        if (value)
            Console.WriteLine(" {0:d} is equals "+
                       "to {1:d}. ", date1, date2);
        else
            Console.WriteLine(" {0:d} is not equals"+
                         " to {1:d}. ", date1, date2);
    }
}

输出:

01/03/2010 is not equals to 01/04/2010. 
 01/05/2010 is not equals to 01/04/2010. 
 01/05/2010 is equals to 01/05/2010. 

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.datetime.equals?view=netframework-4.7.2