CSharp - Write program to Date Value Input

Requirements

You will read a date from a user.

Then dom some simple date arithmetic, add one day to your date value and subtract one day from your date value.

Hint

You will get a DateTime object based on the user input.

You will calculate the next and previous days.

If the user enters a nonexistent day, February 29 of a nonleap year, deal with it using the try-catch construction.

Use the Convert.ToDateTime method to convert string to DateTime value.

Demo

using System;
class Program// ww  w .ja v a2  s.  c  om
{
    static void Main(string[] args)
    {
        try
        {
            // Text input of date 
            Console.Write("Enter date: ");
            string input = Console.ReadLine();

            // Conversion to DateTime object 
            DateTime enteredDate = Convert.ToDateTime(input);

            // Some calculations 
            DateTime followingDay = enteredDate.AddDays(1);
            DateTime previousDay = enteredDate.AddDays(-1);

            // Outputs 
            Console.WriteLine();
            Console.WriteLine("Entered day   : " + enteredDate.ToLongDateString());
            Console.WriteLine("Following day: " + followingDay.ToLongDateString());
            Console.WriteLine("Previous day : " + previousDay.ToLongDateString());
        }
        catch (Exception)
        {
            // Treating incorrect input 
            Console.WriteLine("Incorrect input");
        }

    }
}

Result

Note

You can call the Convert.ToDateTime method with two parameters.

The second parameter is the language setting, which is the CultureInfo object.