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.AddDays() Method in C#
The DateTime.AddDays() method in C# is used to add the specified number of days to the value of this instance. This method returns a new DateTime object without modifying the original instance, as DateTime is immutable.
Syntax
Following is the syntax −
public DateTime AddDays(double days);
Parameters
days − A number of whole and fractional days. The value parameter can be negative or positive.
Return Value
This method returns a new DateTime object whose value is the sum of the date and time represented by this instance and the number of days represented by days.
Using AddDays() with Positive Values
Let us see an example to add days to a DateTime instance −
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 11, 2, 8, 0, 15);
DateTime d2 = d1.AddDays(25);
Console.WriteLine("Initial DateTime = {0:yyyy-MM-dd HH:mm:ss}", d1);
Console.WriteLine("New DateTime (After adding days) = {0:yyyy-MM-dd HH:mm:ss}", d2);
}
}
The output of the above code is −
Initial DateTime = 2019-11-02 08:00:15 New DateTime (After adding days) = 2019-11-27 08:00:15
Using AddDays() with Negative Values
You can subtract days by passing a negative value to AddDays() −
using System;
public class Demo {
public static void Main() {
DateTime currentDate = new DateTime(2023, 6, 15);
DateTime pastDate = currentDate.AddDays(-30);
Console.WriteLine("Current Date = {0:yyyy-MM-dd}", currentDate);
Console.WriteLine("Date 30 days ago = {0:yyyy-MM-dd}", pastDate);
}
}
The output of the above code is −
Current Date = 2023-06-15 Date 30 days ago = 2023-05-16
Using AddDays() with Fractional Values
The method accepts fractional days, allowing you to add hours or minutes indirectly −
using System;
public class Demo {
public static void Main() {
DateTime baseDate = new DateTime(2023, 1, 1, 12, 0, 0);
DateTime newDate = baseDate.AddDays(1.5); // 1 day and 12 hours
Console.WriteLine("Base DateTime = {0:yyyy-MM-dd HH:mm:ss}", baseDate);
Console.WriteLine("After adding 1.5 days = {0:yyyy-MM-dd HH:mm:ss}", newDate);
}
}
The output of the above code is −
Base DateTime = 2023-01-01 12:00:00 After adding 1.5 days = 2023-01-03 00:00:00
Common Use Cases
Date calculations − Adding business days, calculating future dates.
Expiration dates − Setting dates for trials, subscriptions, or temporary access.
Scheduling − Computing recurring event dates or appointment scheduling.
Time-based filtering − Creating date ranges for data queries or reports.
Conclusion
The DateTime.AddDays() method provides a simple way to perform date arithmetic by adding or subtracting days from a DateTime instance. It supports both whole and fractional days, making it versatile for various date calculation scenarios while maintaining immutability.
