Programming Code Examples

How to add months to DateTime in C#2 min read

The following c# example code demonstrate us how can we add specified months with a DateTime object programmatically at run time in an application. .Net framework’s has a built in method DateTime.AddMonths() to add one or more months with a DateTime instance.

DateTime.AddMonths() method return a new DateTime object that add the specified number of months to the value of this instance. This method require to pass a parameter named ‘months’. The ‘months’ parameter value data type is System.Int32. This integer value represent a number of months to add with a date. This parameter value can be negative or positive. So, we can technically add or subtract/remove months from a date.

DateTime.AddMonths() method return a value which data type is System.DateTime. This return value is the sum of the date and time represented by this instance and number of months passed by parameter.

DateTime.AddMonths() through ArgumentOutOfRangeException if the resulting DateTime object is less than MinValue or greater than MaxValue or passed number of months is less than -120000 or greater than 120000.

How to add months to DateTime
How to add months to DateTime



Add comment