Skip to content
Related Articles

Related Articles

Improve Article

DateTime.Equals() Method in C#

  • Last Updated : 28 Jan, 2019

This method is used to get a value indicating whether two DateTime objects, or a DateTime instance and another object or DateTime, have the same value. There are total 3 methods in the overload list of this method:

  • Equals(DateTime, DateTime)
  • Equals(DateTime)
  • Equals(Object)

Equals(DateTime, DateTime)

This method is used to return a value indicating whether two DateTime instances have the same date and time value.



Syntax:

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

Parameters:

  • t1: The first object to compare.
  • t2: The second object to compare.

Return Value: This method returns true if the two values are equal; otherwise, false.

Below programs illustrate the use of DateTime.Equals(DateTime, DateTime) Method:

Example 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. ");
    }
}

Output:

date1 is not equals to date2.

Example 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);
    }
}

Output:



 
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. 

Equals(DateTime)

This method is used to return a value indicating whether the value of this instance is equal to the value of the specified DateTime instance.

Syntax:

public bool Equals (DateTime value);

Here, it takes the object to compare to this instance.

Return Value: This method returns true if the value parameter equals the value of this instance; otherwise, false.

Below programs illustrate the use of DateTime.Equals(DateTime) Method:

Example 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. ");
    }
}

Output:

date1 is not equals to date2. 

Example 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);
    }
}

Output:

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. 

Equals(Object)

This method is used to returns a value indicating whether this instance is equal to a specified object.

Syntax:

public override bool Equals (object value);

Here, it takes the object to compare to this instance.

Return Value: This method returns true if the value parameter equals the value of this instance otherwise it returns false.

Below programs illustrate the use of DateTime.Equals(Object) Method:

Example 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. ");
    }
}

Output:

date1 is not equals to date2. 

Example 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);
    }
}

Output:

 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. 

Reference:




My Personal Notes arrow_drop_up
Recommended Articles
Page :