compare datetimes c# code example
Example 1: c# web form compare dates
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;
if (result < 0)
relationship = "is earlier than";
else if (result == 0)
relationship = "is the same time as";
else
relationship = "is later than";
Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
Example 2: how to compare datetime in c#
DateTime.Compare(datetime1, datetime2);
Example 3: 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 4: c# how to compare 2 dates without time
if(dateTime1.Date == dateTime2.Date)
if (dateTime1.Date.CompareTo(dateTime2.Date))
{
}