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

The following c# example code demonstrate us how can we add one or more years with a DateTime object programmatically at run time in an application. .Net framework’s DateTime Class has a built in method to add years with a date.

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

DateTime.AddYears() method return value data type is System.DateTime. The return value represent a DateTime that is the sum of the date and time of this instance and the number of years pass by parameter.

DateTime.AddYears() method through ArgumentOutOfRangeException if the parameter or the resulting DateTime is less than MinValue or greater than MaxValue.

Example Output:

How to add years to DateTime in C#

How to add years to DateTime in C#


Leave a Comment