DateTime.Add() Method in C#
This method is used to return a new DateTime that adds the value of the specified TimeSpan to the value of this instance.
public DateTime Add (TimeSpan value);
Here, value is a positive or negative time interval.
Return Value: This method returns an object whose value is the sum of the date and time represented by this instance and the time interval represented by value.
Exceptions: This method will give ArgumentOutOfRangeException if the resulting DateTime is less than MinValue or greater than MaxValue.
Below programs illustrate the use of DateTime.Add(TimeSpan) Method:
DateTime before operation: 2010 January 01 DateTime after operation: 2010 February 06
Example 2: For ArgumentOutOfRangeException
DateTime before operation: 0001 January 01 The resulting DateTime is less than the MinValue Exception Thrown: System.ArgumentOutOfRangeException
- The Add method takes into account leap years and the number of days in a month when performing date arithmetic.
- This method does not change the value of this DateTime. Instead, it returns a new DateTime whose value is the result of this operation.