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:

CSharp-program-to-compare-two-dates

CSharp-program-to-compare-two-dates

CSharp-program-to-compare-two-dates

 

 
We are nothing without our users ! You can help us offer even more high quality content. Please share our page !
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
5 + 7 =
 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2022 CodingFusion
75 Important SQL Server queries you should know