当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# DateTime.Equals()用法及代码示例


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

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


等于(DateTime,DateTime)

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

用法:

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

参数:

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

返回值:如果两个值相等,则此方法返回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. 

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 DateTime.Equals() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。