Vous pouvez utiliser Equals ou CompareTo .
Equals : renvoie une valeur indiquant si deux instances DateTime ont la même valeur de date et d'heure.
CompareTo Return Value :
- Inférieur à zéro : si cette instance est antérieure à la valeur.
- Zéro : si cette instance est identique à la valeur.
- Supérieur à zéro : si cette instance est postérieure à la valeur.
DateTime est 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");
}
ou
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 n'est pas annulable:
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");
}
ou
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;
}