DateTime.Equals() Method in C#
Last Updated :
16 Dec, 2021
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#
// 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 equals to date2. ");
else
Console.Write("date1 is not equals to date2. ");
}
}
Output:
date1 is not equals to date2.
Example 2:
C#
// 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#
// 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 equals to date2. ");
else
Console.Write("date1 is not equals to date2. ");
}
}
Output:
date1 is not equals to date2.
Example 2:
C#
// 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 return 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#
// 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 equals to date2. ");
else
Console.Write("date1 is not equals to date2. ");
}
}
Output:
date1 is not equals to date2.
Example 2:
C#
// 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:
Similar Reads
DateTimeOffset.EqualsExact() Method in C#
DateTimeOffset.EqualsExact(DateTimeOffset) Method is used to determine whether the current DateTimeOffset object represents the same time and has the same offset as a specified DateTimeOffset object. Syntax: public bool EqualsExact (DateTimeOffset other); Here, it takes the object to compare to the
2 min read
DateTime.GetHashCode() Method in C#
This method is used to return the hash code for this instance. Syntax: public override int GetHashCode (); Return Value: This method returns a 32-bit signed integer hash code. Below programs illustrate the use of DateTime.GetHashCode() Method: Example 1: csharp // C# program to demonstrate the // Da
1 min read
DateTime.DaysInMonth() Method in C#
This method returns the number of days in the specified month and year. This method always interprets month and year as the month and year of the Gregorian calendar even if the Gregorian calendar is not the current culture's current calendar. Syntax: public static int DaysInMonth (int year, int mont
2 min read
DateTime.Add() Method in C#
This method is used to return a new DateTime that adds the value of the specified TimeSpan to the value of this instance. Syntax: public DateTime Add (TimeSpan value); Here, value is a positive or negative time interval. Return Value: This method returns an object whose value is the sum of the date
2 min read
DateTime.FromOADate() Method in C#
DateTime.FromOADate(Double) Method is used to return a DateTime equivalent to the specified OLE Automation Date. Syntax: public static DateTime FromOADate (double d); Here, it takes an OLE Automation Date value. Return Value: This method returns an object that represents the same date and time as d.
2 min read
DateTime.FromBinary() Method in C#
DateTime.FromBinary(Int64) Method is used to deserialize a 64-bit binary value and recreates an original serialized DateTime object. Syntax: public static DateTime FromBinary (long dateData); Here, it takes a 64-bit signed integer that encodes the Kind property in a 2-bit field and the Ticks propert
2 min read
DateTime.GetTypeCode() Method in C#
This method is used to return the TypeCode for value type DateTime. Syntax: public TypeCode GetTypeCode (); Return Value: This method returns the enumerated constant, DateTime. Below programs illustrate the use of DateTime.GetTypeCode() Method Example 1: csharp // C# program to demonstrate the // Da
1 min read
DateTimeOffset.GetHashCode Method in C#
DateTimeOffset.GetHashCode Method is used to get the hash code for the current DateTimeOffset object. Syntax: public override int GetHashCode (); Return Value: This method returns a 32-bit signed integer hash code. Below programs illustrate the use of DateTimeOffset.GetHashCode() Method: Example 1:
2 min read
DateTimeOffset.Add() Method in C#
This method is used to return a new DateTimeOffset object that adds a specified time interval to the value of this instance. Syntax: public DateTimeOffset Add (TimeSpan timeSpan); Here, it takes a TimeSpan object that represents a positive or a negative time interval.Return Value: This method return
2 min read
DateTime.Compare() Method in C#
This method is used to compare two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance. Syntax: public static int Compare (DateTime t1, DateTime t2); Parameters: t1: The first object to compare. t2: Th
2 min read