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
DateTimeOffset.AddMonths() Method in C#
The DateTimeOffset.AddMonths() method in C# is used to add a specified number of months to a DateTimeOffset instance. This method returns a new DateTimeOffset object with the month value adjusted while preserving the time zone offset information.
Syntax
Following is the syntax for the AddMonths() method −
public DateTimeOffset AddMonths(int months);
Parameters
months − An integer representing the number of months to add. Use a positive value to add months or a negative value to subtract months.
Return Value
Returns a new DateTimeOffset object that represents the date and time that is the specified number of months later (or earlier if negative) than the original instance.
Using AddMonths() to Add Months
The following example demonstrates adding months to a DateTimeOffset instance −
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 08, 10, 4, 20, 10, new TimeSpan(-5, 0, 0));
Console.WriteLine("DateTimeOffset (before adding months) = {0}", dateTimeOffset);
DateTimeOffset res = dateTimeOffset.AddMonths(3);
Console.WriteLine("DateTimeOffset (after adding months) = {0}", res);
}
}
The output of the above code is −
DateTimeOffset (before adding months) = 8/10/2019 4:20:10 AM -05:00 DateTimeOffset (after adding months) = 11/10/2019 4:20:10 AM -05:00
Using AddMonths() to Subtract Months
You can subtract months by passing a negative value to the AddMonths() method −
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 08, 10, 4, 20, 10, new TimeSpan(-5, 0, 0));
Console.WriteLine("DateTimeOffset (before subtracting months) = {0}", dateTimeOffset);
DateTimeOffset res = dateTimeOffset.AddMonths(-5);
Console.WriteLine("DateTimeOffset (after subtracting months) = {0}", res);
}
}
The output of the above code is −
DateTimeOffset (before subtracting months) = 8/10/2019 4:20:10 AM -05:00 DateTimeOffset (after subtracting months) = 3/10/2019 4:20:10 AM -05:00
Handling Month-End Dates
When adding months to dates at the end of a month, the AddMonths() method automatically adjusts for months with fewer days −
using System;
public class Demo {
public static void Main() {
DateTimeOffset endOfJanuary = new DateTimeOffset(2024, 1, 31, 10, 0, 0, new TimeSpan(0, 0, 0));
Console.WriteLine("Original date: {0}", endOfJanuary);
DateTimeOffset addedOneMonth = endOfJanuary.AddMonths(1);
Console.WriteLine("After adding 1 month: {0}", addedOneMonth);
DateTimeOffset addedTwoMonths = endOfJanuary.AddMonths(2);
Console.WriteLine("After adding 2 months: {0}", addedTwoMonths);
}
}
The output of the above code is −
Original date: 1/31/2024 10:00:00 AM +00:00 After adding 1 month: 2/29/2024 10:00:00 AM +00:00 After adding 2 months: 3/31/2024 10:00:00 AM +00:00
Conclusion
The DateTimeOffset.AddMonths() method provides a convenient way to add or subtract months from a DateTimeOffset instance while preserving the time zone offset. The method automatically handles month-end scenarios by adjusting to the last valid day of the target month when necessary.
