DateTimeOffset.AddYears() Method in C#

The DateTimeOffset.AddYears() method in C# is used to add a specified number of years to a DateTimeOffset instance. This method returns a new DateTimeOffset object representing the date and time with the added years, while preserving the original offset from UTC.

Syntax

Following is the syntax for the DateTimeOffset.AddYears() method −

public DateTimeOffset AddYears(int years);

Parameters

The method takes one parameter −

  • years − An integer representing the number of years to add. Use positive values to add years, negative values to subtract years.

Return Value

The method returns a new DateTimeOffset object with the specified number of years added to the original instance. The offset component remains unchanged.

Adding Years to DateTimeOffset

Example

using System;

public class Demo {
    public static void Main() {
        DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 06, 09, 8, 20, 10, new TimeSpan(-5, 0, 0));
        Console.WriteLine("DateTimeOffset (before adding years) = {0}", dateTimeOffset);
        
        DateTimeOffset res = dateTimeOffset.AddYears(5);
        Console.WriteLine("DateTimeOffset (after adding years) = {0}", res);
    }
}

The output of the above code is −

DateTimeOffset (before adding years) = 6/9/2019 8:20:10 AM -05:00
DateTimeOffset (after adding years) = 6/9/2024 8:20:10 AM -05:00

Subtracting Years from DateTimeOffset

Example

using System;

public class Demo {
    public static void Main() {
        DateTimeOffset dateTimeOffset = new DateTimeOffset(2020, 12, 25, 15, 30, 45, new TimeSpan(2, 0, 0));
        Console.WriteLine("DateTimeOffset (before subtracting years) = {0}", dateTimeOffset);
        
        DateTimeOffset res = dateTimeOffset.AddYears(-3);
        Console.WriteLine("DateTimeOffset (after subtracting years) = {0}", res);
    }
}

The output of the above code is −

DateTimeOffset (before subtracting years) = 12/25/2020 3:30:45 PM +02:00
DateTimeOffset (after subtracting years) = 12/25/2017 3:30:45 PM +02:00

Handling Leap Year Edge Cases

Example

using System;

public class Demo {
    public static void Main() {
        // February 29th in a leap year
        DateTimeOffset leapDate = new DateTimeOffset(2020, 02, 29, 10, 0, 0, TimeSpan.Zero);
        Console.WriteLine("Original leap year date: {0}", leapDate);
        
        // Adding 1 year (2021 is not a leap year)
        DateTimeOffset result = leapDate.AddYears(1);
        Console.WriteLine("After adding 1 year: {0}", result);
        
        // Adding 4 years (2024 is a leap year)
        DateTimeOffset result2 = leapDate.AddYears(4);
        Console.WriteLine("After adding 4 years: {0}", result2);
    }
}

The output of the above code is −

Original leap year date: 2/29/2020 10:00:00 AM +00:00
After adding 1 year: 2/28/2021 10:00:00 AM +00:00
After adding 4 years: 2/29/2024 10:00:00 AM +00:00

Key Points

  • The method preserves the time component and UTC offset of the original DateTimeOffset.

  • When adding years to February 29th and the target year is not a leap year, the result becomes February 28th.

  • The original DateTimeOffset instance remains unchanged − a new instance is returned.

Conclusion

The DateTimeOffset.AddYears() method provides a convenient way to perform year arithmetic on date-time values while maintaining the UTC offset. It handles leap year edge cases automatically, making it reliable for date calculations in applications dealing with time zones.

Updated on: 2026-03-17T07:04:35+05:30

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements