DateTime.AddMonths() Method in C#
This method is used to return a new DateTime that adds the specified number of months to the value of this instance.
C# Code:
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(2020, 1, 1, 0, 0, 0); // adding the 2 months // using AddMonths() method; DateTime date2 = date1.AddMonths(2); 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(); } |
Example 2: C# DateTime to add months 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 2 Months = {0}", DateTime.Today.AddMonths(2)); Console.ReadKey(); } |