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.
- Difference between Method Overriding and Method Hiding in C#
- C# | Clone() Method
- C# | Math.Abs() Method | Set - 1
- C# | Math.Abs() Method | Set - 2
- C# | Math.Exp() Method
- C# | IndexOfAny() Method
- C# | Math.Sin() Method
- Decimal.Add() Method in C#
- C# | Dictionary.Add() Method
- C# | Math.Cos() Method
- C# | Math.Tan() Method
- C# | String.Contains() Method
- C# | Math.Min() Method
- C# | Insert() Method
- C# | Random.Next() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.