Puede usar Equals o CompareTo .
Igual : devuelve un valor que indica si dos DateTime instancias tienen el mismo valor de fecha y hora.
Comparar para devolver el valor :
- Menos de cero : si esta instancia es anterior al valor.
- Cero : si esta instancia es igual al valor.
- Mayor que cero : si esta instancia es posterior al valor.
DateTime es anulable:
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");
}
o
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 no es anulable:
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");
}
o
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;
}