Export (0) Print
Expand All

DateTime.Add Method

Adds the value of the specified TimeSpan to the value of this instance.

[Visual Basic]
Public Function Add( _
   ByVal value As TimeSpan _
) As DateTime
[C#]
public DateTime Add(
 TimeSpan value
);
[C++]
public: DateTime Add(
 TimeSpan value
);
[JScript]
public function Add(
   value : TimeSpan
) : DateTime;

Parameters

value
A TimeSpan that contains the interval to add.

Return Value

A DateTime whose value is the sum of the date and time represented by this instance and the time interval represented by value.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException The resulting DateTime is less than MinValue or greater than MaxValue.

Remarks

This method does not change the value of this DateTime. Instead, a new DateTime is returned whose value is the result of this operation.

Example

[Visual Basic, C#, C++] The following sample demonstrates the Add method. It calculates the day of the week that is 36 days (864 hours) from this moment.

[Visual Basic] 
' Calculate what day of the week is 36 days from this instant.
Dim today As System.DateTime
Dim duration As System.TimeSpan
Dim answer As System.DateTime

today = System.DateTime.Now
duration = New System.TimeSpan(36, 0, 0, 0)
answer = today.Add(duration)

System.Console.WriteLine("{0:dddd}", answer)

[C#] 
// Calculate what day of the week is 36 days from this instant.
System.DateTime today = System.DateTime.Now;
System.TimeSpan duration = new System.TimeSpan(36, 0, 0, 0);
System.DateTime answer = today.Add(duration);
System.Console.WriteLine("{0:dddd}", answer);

[C++] 
// Calculate what day of the week is 36 days from this instant.
System::DateTime today = System::DateTime::Now;
System::TimeSpan duration(36, 0, 0, 0);
System::DateTime answer = today.Add(duration);
System::Console::WriteLine(S" {0:dddd}", __box(answer));

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

DateTime Structure | DateTime Members | System Namespace | TimeSpan | TimeSpan.Add

Show:
© 2014 Microsoft