You can use Equals or CompareTo.
Equals: Returns a value indicating whether two DateTime instances have the same date and time value.
CompareTo Return Value:
- Less than zero : If this instance is earlier than value.
- Zero : If this instance is the same as value.
- Greater than zero : If this instance is later than value.
DateTime is nullable:
DateTime? first = new DateTime(1992,02,02,20,50,1);
DateTime? second = new DateTime(1992, 02, 02, 20, 50, 2);
if (first.Value.Date.Equals(second.Value.Date))
{
Console.WriteLine("Equal");
}
or
DateTime? first = new DateTime(1992,02,02,20,50,1);
DateTime? second = new DateTime(1992, 02, 02, 20, 50, 2);
var compare = first.Value.Date.CompareTo(second.Value.Date);
switch (compare)
{
case 1:
Console.WriteLine("this instance is later than value.");
break;
case 0:
Console.WriteLine("this instance is the same as value.");
break;
default:
Console.WriteLine("this instance is earlier than value.");
break;
}
DateTime is not nullable:
DateTime first = new DateTime(1992,02,02,20,50,1);
DateTime second = new DateTime(1992, 02, 02, 20, 50, 2);
if (first.Date.Equals(second.Date))
{
Console.WriteLine("Equal");
}
or
DateTime first = new DateTime(1992,02,02,20,50,1);
DateTime second = new DateTime(1992, 02, 02, 20, 50, 2);
var compare = first.Date.CompareTo(second.Date);
switch (compare)
{
case 1:
Console.WriteLine("this instance is later than value.");
break;
case 0:
Console.WriteLine("this instance is the same as value.");
break;
default:
Console.WriteLine("this instance is earlier than value.");
break;
}