C# program to compare two dates
In this C# program, we will compare two dates and print the result.
C# program to compare two dates Code:
private static void Main(string[] args)
{
DateTime date1 = new DateTime(2022, 2, 1); //---- Year,Month,Date
DateTime date2 = new DateTime(2022, 1, 1); //---- Year,Month,Date
int result = DateTime.Compare(date1, date2);
if (result > 0)
{
Console.WriteLine("date1 {0} is greater than date2 {1}.", date1, date2);
}
else if (result < 0)
{
Console.WriteLine("date1 {0} is less than date2 {1}.", date1, date2);
}
else
{
Console.WriteLine("date1 {0} is the same as date2. {1}", date1, date2);
}
Console.ReadLine();
}
C# program to compare two dates Output: