How to compare two DateTime objects in C#? - Biz Tech

How to compare two DateTime objects in C#?

Listen
DateTime date1 = new DateTime(2022, 3, 18);
DateTime date2 = new DateTime(2022, 3, 11);
int result = DateTime.Compare(date1, date2);
if (result > 0)
{
    Console.WriteLine("date1 is later than date2");
}
else if (result < 0)
{
    Console.WriteLine("date1 is earlier than date2");
}
else
{
    Console.WriteLine("date1 is equal to date2");
}