C# Program to Compare Two Dates

In this C# program, we start by providing a welcome message and prompting the user to enter two dates in the format “yyyy-MM-dd”. We then use DateTime.Parse to convert the user input into DateTime objects.

Problem statement

Write a C# program that prompts the user to enter two dates and then compares them.

C# Program to Compare Two Dates

using System;

class Program
{
    static void Main()
    {
        // Define two dates
        DateTime date1 = new DateTime(2023, 9, 9);
        DateTime date2 = new DateTime(2023, 9, 10);

        // Compare the dates
        int result = DateTime.Compare(date1, date2);

        if (result < 0)
        {
            Console.WriteLine("{0} is earlier than {1}", date1, date2);
        }
        else if (result == 0)
        {
            Console.WriteLine("{0} is the same as {1}", date1, date2);
        }
        else
        {
            Console.WriteLine("{0} is later than {1}", date1, date2);
        }
    }
}

How it works

Here’s a breakdown of how the program works:

  1. Welcome Message and User Input:
    • The program starts by displaying a welcome message using Console.WriteLine("Welcome to the Date Comparison Program!");.
    • It then prompts the user to enter the first date using Console.Write("Enter the first date (yyyy-MM-dd): ");.
    • The user’s input is read using Console.ReadLine() and converted to a DateTime object using DateTime.Parse().
  2. Second Date Input:
    • The program prompts the user to enter the second date in a similar manner as the first date.
  3. Date Comparison:
    • The program uses DateTime.Compare(date1, date2) to compare the two dates. This method returns:
      • A value less than 0 if date1 is earlier than date2.
      • 0 if date1 is the same as date2.
      • A value greater than 0 if date1 is later than date2.
  4. Comparison Result Handling:
    • Using an if-else statement, the program checks the result of the comparison and prints out an appropriate message:
      • If result is less than 0, it means date1 is earlier than date2.
      • If result is 0, it means both dates are the same.
      • If result is greater than 0, it means date1 is later than date2.
  5. Output:
    • The program displays the comparison result message using Console.WriteLine().
  6. Graceful Error Handling:
    • The program should ideally include additional error handling to account for cases where the user provides invalid date formats. This example program assumes that the user provides valid dates.

Example Output:

Code:

Welcome to the Date Comparison Program!

Enter the first date (yyyy-MM-dd): 2023-09-09
Enter the second date (yyyy-MM-dd): 2023-09-10

2023-09-09 is earlier than 2023-09-10

This program is designed to take two dates from the user, compare them, and then display a message indicating which date comes first, or if they are the same. Keep in mind that it’s always a good practice to add additional error handling to handle cases where the user provides invalid inputs.

Input/Output

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...