c# compare datetime only date code example
Example 1: how to compare datetime in c#
DateTime.Compare(datetime1, datetime2);
Example 2: c# how does comparing datetime work
using System;
public class Demo {
public static void Main(){
DateTime d1 = new DateTime(2019, 12, 20, 6, 20, 40);
DateTime d2 = new DateTime(2019, 11, 20, 6, 20, 40);
Console.WriteLine("DateTime 1 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1);
Console.WriteLine("DateTime 2 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2);
int res = DateTime.Compare(d1, d2);
Console.WriteLine(res);
}
}
Example 3: c# how to compare 2 dates without time
if(dateTime1.Date == dateTime2.Date)
if (dateTime1.Date.CompareTo(dateTime2.Date))
{
}