C# Program to Compare Two Dates

This is a C# Program to compare two dates.

Problem Description

This C# Program Compares Two Dates.

Problem Solution

Here two dates are compared and the date which occurs first is displayed.

Program/Source Code

Here is source code of the C# Program to Compare Two Dates. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Campare Two Dates
 */
using System;
namespace DateAndTime
{
    class Program
    {
        static int Main()
        {
            DateTime sd = new DateTime(2010, 10, 12);
            Console.WriteLine("Starting Date : {0}", sd);
            DateTime ed = sd.AddDays(10);
            Console.WriteLine("Ending Date   : {0}", ed);
            if (sd < ed)
                Console.WriteLine("{0} Occurs Before {1}", sd, ed);
            Console.Read();
            return 0;
 
        }
    }
}
Program Explanation

In this C# program, the DateTime() constructor is used to create the format of the date. If condition statement is used to compare the two dates and the date which occurs first is displayed.

advertisement
advertisement
Runtime Test Cases
 
Starting Date : 10/11/2010  12:00:00 AM
Ending Date   : 10/21/2010  12:00:00 AM
10/11/2010 12:00:00 Am Occurs Before 10/21/2010 12:00:00 AM

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.