CSharp - Write program to Date Value Input


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.


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.


using System;
class Program/* www.ja v  a  2s.com*/
    static void Main(string[] args)
            // 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("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");




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

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