C# DateTime Add Months

In this article, learn how to add months to the date object. The DateTime.AddMonths() Method used to return a new DateTime that adds the specified number of months to the value of this instance


C# DateTime Add Months

static void Main(string[] args)
{
      DateTime date1 = new DateTime(2022, 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.ReadLine();
}

C# DateTime to add months to the current date

 
 
static void Main(string[] args)
{
    Console.WriteLine("Today = {0}", DateTime.Today);
    Console.WriteLine("Add 2 Months = {0}", DateTime.Today.AddMonths(2));
 
   Console.ReadLine();
}


– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Share this:
We will be happy to hear your thoughts

      Leave a reply

      www.troubleshootyourself.com
      Logo