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.

Syntax

Following is the syntax −

public DateTime AddDays (double days);

Above, the parameter days are the number of days to be added. To subtract, add a negative value.

Example

Let us now see an example to implement the DateTime.AddDays() method −

using System;
public class Demo {
   public static void Main(){
      DateTime d1 = new DateTime(2019, 11, 2, 8, 0, 15);
      DateTime d2 = d1.AddDays(25);
      System.Console.WriteLine("Initial DateTime = {0:y} {0:dd}", d1);
      System.Console.WriteLine("\nNew DateTime (After adding days) = {0:y} {0:dd}", d2);
   }
}

Output

This will produce the following output −

Initial DateTime = November 2019 02
New DateTime (After adding days) = November 2019 27

Example

Let us now see another example to implement the DateTime.AddDays() method −

using System;
public class Demo {
   public static void Main(){
      DateTime d1 = DateTime.MinValue;
      DateTime d2 = d1.AddDays(150);
      System.Console.WriteLine("Initial DateTime = {0:y} {0:dd}", d1);
      System.Console.WriteLine("\nNew DateTime (After adding days) = {0:y} {0:dd}", d2);
   }
}

Output

This will produce the following output −

Initial DateTime = January 0001 01
New DateTime (After adding days) = May 0001 31
raja
Published on 06-Nov-2019 05:33:31
Advertisements