Introducing Radical.sh

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

Adding days to a date in C#

We can add days to a date like below.
C# will take care of month and year when you add the days since it adheres to universal date and time rules.

  1. using System;
  2.  
  3. namespace forgetCode
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9.  
  10. Console.WriteLine("Enter year in the format dd-mm-yyyy");
  11. DateTime dt = Convert.ToDateTime(Console.ReadLine());
  12.  
  13. Console.WriteLine("Enter the days to add :");
  14. int days = Convert.ToInt32(Console.ReadLine());
  15.  
  16. DateTime newDate = dt.AddDays(days);
  17. Console.WriteLine(newDate.ToShortDateString());
  18. }
  19.  
  20. }
  21. }


Output:
Enter year in the format dd-mm-yyyy
25-12-2012
Enter the days to add :
7
01-01-2013

..