Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
DateTime.AddYears() Method in C#
The DateTime.AddYears() method in C# adds a specified number of years to a DateTime instance and returns a new DateTime object. This method is particularly useful for calculating future or past dates, handling anniversaries, or working with year-based intervals.
Syntax
Following is the syntax for the AddYears() method −
public DateTime AddYears(int value);
Parameters
The method accepts a single parameter −
value − An integer representing the number of years to add. Use positive values to add years and negative values to subtract years.
Return Value
Returns a new DateTime object with the specified number of years added to the original date. The original DateTime instance remains unchanged.
Adding Years to a DateTime
Example
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 11, 20, 6, 20, 40);
DateTime d2 = d1.AddYears(5);
Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d1);
Console.WriteLine("New DateTime (added years) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d2);
}
}
The output of the above code is −
Initial DateTime = 20 November 2019, 06:20:40 New DateTime (added years) = 20 November 2024, 06:20:40
Subtracting Years from a DateTime
You can subtract years by passing a negative value to the AddYears() method −
Example
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 11, 20, 6, 20, 40);
DateTime d2 = d1.AddYears(-2);
Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d1);
Console.WriteLine("New DateTime (subtracting years) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d2);
}
}
The output of the above code is −
Initial DateTime = 20 November 2019, 06:20:40 New DateTime (subtracting years) = 20 November 2017, 06:20:40
Handling Leap Year Edge Cases
When working with February 29th (leap year), the AddYears() method automatically adjusts to February 28th if the target year is not a leap year −
Example
using System;
public class Demo {
public static void Main() {
DateTime leapYearDate = new DateTime(2020, 2, 29); // Feb 29, 2020 (leap year)
DateTime newDate = leapYearDate.AddYears(1); // Add 1 year
Console.WriteLine("Original Date: {0:yyyy-MM-dd}", leapYearDate);
Console.WriteLine("After Adding 1 Year: {0:yyyy-MM-dd}", newDate);
Console.WriteLine("Note: Feb 29 becomes Feb 28 in non-leap year");
}
}
The output of the above code is −
Original Date: 2020-02-29 After Adding 1 Year: 2021-02-28 Note: Feb 29 becomes Feb 28 in non-leap year
Conclusion
The DateTime.AddYears() method provides a simple way to add or subtract years from a date while preserving the time component. It automatically handles leap year adjustments, making it reliable for date calculations involving year intervals.
