DateTime.AddDays() Method in C#
This method is used to return a new DateTime that adds the specified number of days to the value of this instance.
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
static void Main(string[] args) { DateTime date1 = new DateTime(2010, 1, 1, 0,0,0); // adding the 10 days // using AddDays() method; DateTime date2 = date1.AddDays(10); Console.WriteLine("DateTime before " +"operation: {0:y} {0:dd}", date1); // Display the date2 Console.WriteLine("\nDateTime after" +" operation: {0:y} {0:dd}", date2); Console.ReadKey(); } |
Output:
1 2 3 4 5 |
DateTime before operation: 2010 January 01 DateTime after operation: 2010 January 11 |
Example 2: C# DateTime to add days to the current date
1 2 3 4 5 6 7 8 9 |
static void Main(string[] args) { Console.WriteLine("Today = {0}", DateTime.Today); Console.WriteLine("Add 10 Days = {0}", DateTime.Today.AddDays(10)); Console.ReadKey(); } |
Output: