C# Program to Add Two Dates

This C# program is designed to add two dates together. It allows users to input two dates in the “YYYY-MM-DD” format and then calculates their sum, considering only the time component. The result is displayed in a human-readable format.

Problem statement

You are tasked with creating a C# program that adds two dates together. The program should prompt the user to input two dates in the format “YYYY-MM-DD”, add them together, and then display the sum.

C# Program to Add Two Dates

using System;

class Program
{
    static void Main()
    {
        // Input two dates
        Console.WriteLine("Enter the first date (YYYY-MM-DD):");
        DateTime date1 = DateTime.Parse(Console.ReadLine());

        Console.WriteLine("Enter the second date (YYYY-MM-DD):");
        DateTime date2 = DateTime.Parse(Console.ReadLine());

        // Add the dates
        DateTime sum = date1.Add(date2 - date2.Date);

        // Print the result
        Console.WriteLine("The sum of the dates is: " + sum.ToString("yyyy-MM-dd HH:mm:ss"));
    }
}

How it works

Here’s an explanation of how the C# program for adding two dates works, step by step:

  1. Introduction Message:
    • The program starts by displaying a welcome message and an explanation of its purpose. This message informs the user about the program’s functionality.
  2. User Input:
    • After the introduction, the program prompts the user to enter two dates.
    • The user is instructed to enter each date in the format “YYYY-MM-DD” (Year, Month, Day).
  3. Date Input and Parsing:
    • The program uses the DateTime.Parse() method to read and parse the user’s input as DateTime objects. These objects represent the first and second dates provided by the user.
  4. Date Addition:
    • To add the two dates together, the program uses the Add() method of the DateTime structure.
    • It calculates the sum by adding the second date (date2) to the first date (date1), but with the date component of date2 subtracted. This subtraction ensures that only the time component is added.
  5. Formatting and Display:
    • The result of the addition is a DateTime object that represents the sum of the two dates, including both the date and time components.
    • The program then formats this DateTime object into a string with the format “YYYY-MM-DD HH:mm:ss” using the ToString() method. This format ensures that the date and time are displayed in a human-readable format.
  6. Output:
    • Finally, the program displays the sum of the dates in the specified format.
  7. Note on Exception Handling:
    • The problem statement includes a note about handling exceptions. In practice, you should include error-checking and exception-handling code to handle cases where the user enters invalid date formats or inputs that cannot be parsed as DateTime objects. This ensures that your program is robust and doesn’t crash when faced with unexpected input.

In summary, the program takes two dates as input, adds them together while considering only the time component, and then presents the result in a specific format to the user.

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...