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# | String.Contains() Method
- C# | Math.Abs() Method | Set - 2
- C# | Math.Min() Method
- C# | Uri.ToString() Method
- C# | Uri.IsWellFormedOriginalString() Method
- C# | ToCharArray() Method
- C# | Copy() Method
- C# | ToUpper() Method
- Anonymous Method in C#
- C# | ToLower() Method
- C# | Dictionary.Add() Method
- C# | Uri.IsHexEncoding() Method
- C# | Math.Tan() Method
- C# | Method Overloading
- C# | Join() Method | Set - 1
- C# | IsNullOrWhiteSpace() Method
- C# | Math.Sin() Method
- C# | Math.Cos() Method
- C# | PadLeft() 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.