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.AddDays() Method in C#
The DateTimeOffset.AddDays() method in C# returns a new DateTimeOffset object that adds a specified number of whole and fractional days to the value of the current instance. This method allows you to add or subtract days while preserving the original timezone offset information.
Syntax
Following is the syntax −
public DateTimeOffset AddDays(double days);
Parameters
days − A number of whole and fractional days. The value parameter can be negative or positive.
Return Value
Returns a new DateTimeOffset object whose value is the sum of the date and time represented by the current instance and the number of days represented by the days parameter.
Adding Days to DateTimeOffset
Example
The following example demonstrates adding 20 days to a DateTimeOffset object −
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 10, 10, 9, 15, 0, new TimeSpan(-5, 0, 0));
DateTimeOffset res = dateTimeOffset.AddDays(20);
Console.WriteLine("Original DateTimeOffset = {0}", dateTimeOffset);
Console.WriteLine("After adding 20 days = {0}", res);
}
}
The output of the above code is −
Original DateTimeOffset = 10/10/2019 9:15:00 AM -05:00 After adding 20 days = 10/30/2019 9:15:00 AM -05:00
Subtracting Days from DateTimeOffset
Example
The following example shows how to subtract days by passing a negative value −
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 10, 10, 9, 15, 0, new TimeSpan(-5, 0, 0));
DateTimeOffset res = dateTimeOffset.AddDays(-5);
Console.WriteLine("Original DateTimeOffset = {0}", dateTimeOffset);
Console.WriteLine("After subtracting 5 days = {0}", res);
}
}
The output of the above code is −
DateTimeOffset (after subtracting days) = 10/5/2019 9:15:00 AM -05:00
Using Fractional Days
Example
The method also accepts fractional days, allowing you to add partial days −
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 10, 10, 9, 15, 0, new TimeSpan(-5, 0, 0));
DateTimeOffset res1 = dateTimeOffset.AddDays(1.5);
DateTimeOffset res2 = dateTimeOffset.AddDays(0.25);
Console.WriteLine("Original DateTimeOffset = {0}", dateTimeOffset);
Console.WriteLine("After adding 1.5 days = {0}", res1);
Console.WriteLine("After adding 0.25 days (6 hours) = {0}", res2);
}
}
The output of the above code is −
Original DateTimeOffset = 10/10/2019 9:15:00 AM -05:00 After adding 1.5 days = 10/11/2019 9:15:00 PM -05:00 After adding 0.25 days (6 hours) = 10/10/2019 3:15:00 PM -05:00
Conclusion
The DateTimeOffset.AddDays() method provides a convenient way to perform date arithmetic while preserving timezone information. It accepts both positive and negative values for adding or subtracting days, and supports fractional values for precise time calculations.
