C# DateTime AddYears() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DateTime

Definition

In C#, DateTime.AddYears moves a date by whole years. A negative number goes back. Month, day, and time stay the same.

The only special case is February 29. If the target year is not a leap year, the day becomes February 28.

This is an instance member of System.DateTime. You call it on a DateTime value, such as dt.AddYears(...). DateTime is a struct, so every value is copied and never changed in place.

Syntax

Pass a whole number of years (an int).

C#
DateTime d = new DateTime(2026, 11, 18);
DateTime a = d.AddYears(5);     // 2031-11-18
DateTime b = d.AddYears(-26);   // 2000-11-18
DateTime c = new DateTime(2028, 2, 29).AddYears(1);   // 2029-02-28
CallWhat it does
d.AddYears(5)Five years later, same month and day.
d.AddYears(-26)Twenty-six years earlier.
new DateTime(2028, 2, 29).AddYears(1)Leap day becomes February 28 in 2029.

The argument is an int. The result must stay within years 1 to 9999, or ArgumentOutOfRangeException is thrown.

Library declarations
C#
public DateTime AddYears(int value);

Parameters

ParameterDescription
valueRequired. Whole number of years to add. Negative values go backward.

Return Value

Return ValueDescription
DateTimeA new DateTime in the target year. February 29 is moved to February 28 if needed.

C# Examples

Example 1 — Add and Subtract Years

Move a date five years ahead and 26 years back.

C#
using System;
using System.Globalization;

class Program {
    static string Day(DateTime d) => d.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

    static void Main() {
        DateTime d = new DateTime(2026, 11, 18);
        Console.WriteLine(Day(d.AddYears(5)));
        Console.WriteLine(Day(d.AddYears(-26)));
    }
}

How It Works

  1. Plus five. Only the year number changes: 2026 becomes 2031.
  2. Minus 26. A negative value reaches the year 2000.
  3. Month and day stay. Both results keep November 18.
  4. Date only. The helper prints the date and hides the midnight time.

Example 2 — February 29

Add one year and four years to a leap day.

C#
using System;
using System.Globalization;

class Program {
    static string Day(DateTime d) => d.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

    static void Main() {
        DateTime leapDay = new DateTime(2028, 2, 29);
        Console.WriteLine(Day(leapDay.AddYears(1)));
        Console.WriteLine(Day(leapDay.AddYears(4)));
    }
}

How It Works

  1. 2029 is not a leap year. February 29 does not exist, so the day becomes 28.
  2. 2032 is a leap year. Four years later February 29 exists again.
  3. No exception. The method picks the nearest valid day instead of failing.
  4. Compare steps. Adding four single years would end at the 28th, not the 29th.

Example 3 — Compute an Age

Use AddYears to find a full age and the next birthday.

C#
using System;
using System.Globalization;

class Program {
    static void Main() {
        DateTime birth = new DateTime(1990, 5, 15);
        DateTime today = new DateTime(2026, 11, 18);
        int age = today.Year - birth.Year;
        if (birth.AddYears(age) > today) age--;
        Console.WriteLine(age);
        Console.WriteLine(birth.AddYears(age + 1).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
    }
}

How It Works

  1. Fixed "today". The code uses November 18, 2026 so the answer never changes.
  2. First guess. 2026 minus 1990 is 36 years.
  3. Birthday check. birth.AddYears(36) is May 15, 2026, which is not after today, so age stays 36.
  4. Next birthday. AddYears(age + 1) returns May 15, 2027.

Frequently Asked Questions

If the target year is not a leap year, the date becomes February 28.
Yes. AddYears(-1) returns the same date one year earlier.
Subtract the years, then subtract one more if birth.AddYears(age) is after today.
Yes. Only the year (and possibly Feb 29) can change.
No. DateTime is immutable. Use the returned value.

Next: Compare()

Compare two DateTime values and get a number: less than, equal to, or greater than zero with System.DateTime.

Compare() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful