DateTime.Add() Method in C#

The DateTime.Add() method in C# is used to return a new DateTime that adds the value of the specified TimeSpan to the value of this instance. This method allows you to add or subtract time intervals including days, hours, minutes, seconds, and milliseconds.

Syntax

Following is the syntax −

public DateTime Add(TimeSpan value);

Parameters

  • value − A positive or negative TimeSpan that represents the time interval to add to the current DateTime instance.

Return Value

Returns a new DateTime object whose value is the sum of the date and time represented by this instance and the time interval represented by value.

DateTime.Add() Operation Original DateTime + TimeSpan Value = New DateTime Original DateTime remains unchanged

Using DateTime.Add() to Add Days

This example demonstrates adding 115 days to a DateTime instance −

using System;

public class Demo {
   public static void Main() {
      DateTime d1 = new DateTime(2019, 3, 7, 8, 0, 15);
      TimeSpan span = new TimeSpan(115, 0, 0, 0);
      DateTime d2 = d1.Add(span);
      Console.WriteLine("Initial DateTime = {0:y} {0:dd}", d1);
      Console.WriteLine("New DateTime = {0:y} {0:dd}", d2);
   }
}

The output of the above code is −

Initial DateTime = March 2019 07
New DateTime = June 2019 30

Using DateTime.Add() to Subtract Days

This example shows how to subtract days by using a negative TimeSpan value −

using System;

public class Demo {
   public static void Main() {
      DateTime d1 = new DateTime(2019, 9, 7, 8, 0, 15);
      // subtracting days using negative TimeSpan
      TimeSpan span = new TimeSpan(-75, 0, 0, 0);
      DateTime d2 = d1.Add(span);
      Console.WriteLine("Initial DateTime = {0:y} {0:dd}", d1);
      Console.WriteLine("New DateTime = {0:y} {0:dd}", d2);
   }
}

The output of the above code is −

Initial DateTime = September 2019 07
New DateTime = June 2019 24

Using DateTime.Add() with Hours and Minutes

This example demonstrates adding hours and minutes to a DateTime −

using System;

public class Demo {
   public static void Main() {
      DateTime original = new DateTime(2023, 12, 25, 9, 30, 0);
      TimeSpan addTime = new TimeSpan(2, 45, 30); // 2 hours, 45 minutes, 30 seconds
      DateTime result = original.Add(addTime);
      
      Console.WriteLine("Original DateTime: " + original.ToString("yyyy-MM-dd HH:mm:ss"));
      Console.WriteLine("Added TimeSpan: " + addTime.ToString());
      Console.WriteLine("Result DateTime: " + result.ToString("yyyy-MM-dd HH:mm:ss"));
   }
}

The output of the above code is −

Original DateTime: 2023-12-25 09:30:00
Added TimeSpan: 02:45:30
Result DateTime: 2023-12-25 12:15:30

Conclusion

The DateTime.Add() method provides a flexible way to add or subtract time intervals from DateTime objects. It accepts positive TimeSpan values for addition and negative values for subtraction, returning a new DateTime instance while keeping the original unchanged.

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

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements