Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Adding months to a date in C#

We can add months to a date like below.
C# will take care of year when you add the months since it adheres to universal date and time rules.
The day will remain same.

using System;

namespace forgetCode
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Enter year in the format dd-mm-yyyy");
            DateTime dt = Convert.ToDateTime(Console.ReadLine());

            Console.WriteLine("Enter the months to add :");
            int months = Convert.ToInt32(Console.ReadLine());

            DateTime newDate = dt.AddMonths(months);
            Console.WriteLine(newDate.ToShortDateString());
       
            
        }

    }
}


Output:
Enter year in the format dd-mm-yyyy
02-03-2012
Enter the months to add :
4
02-07-2012


Enter year in the format dd-mm-yyyy
20-11-2012
Enter the months to add :
2
20-01-2013

..