當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。