compare date c# code example
Example 1: asp net c# compare date to current
string date = "01/04/2013";
DateTime myDate = DateTime.ParseExact(date, "dd/MM/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
if (myDate > DateTime.Today)
{
Console.WriteLine("greater than");
}
else
{
Console.WriteLine("Less Than");
}
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))
{
}