Q:

C# program to compare two dates

0

C# program to compare two dates

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

//Program to Compare two dates in C#.

using System;

class DateCompareDemo
{
    static int Main()
    {
        DateTime D1 = new DateTime(2019, 08, 15);
        DateTime D2 = new DateTime(2020, 10, 12);
       
        if (D1 < D2)
            Console.WriteLine(D1);
        else
            Console.WriteLine(D2);
        
        return 0;
    }
}

Output:

8/15/2019 12:00:00 AM
Press any key to continue . . .

Explanation:

In the above program, we created a class DateCompareDemo that contains the Main() method. In the Main() method we created two objects D1 and D2 of DateTime class that are initialized with date values.

if (D1 < D2)
    Console.WriteLine(D1);
else
    Console.WriteLine(D2);

In the above code, we compared dates using less than "<" operator, in our example D1 is an earlier date then it will be printed on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now