Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

DateTime Addition Operator

Adds a specified time interval to a specified date and time, yielding a new date and time.

[Visual Basic]
returnValue = DateTime.op_Addition(d, t)
[C#]
public static DateTime operator +(
 DateTime d,
 TimeSpan t
);
[C++]
public: static DateTime op_Addition(
 DateTime d,
 TimeSpan t
);
[JScript]
returnValue = d + t;

[Visual Basic] In Visual Basic, you can use the operators defined by a type, but you cannot define your own. You can use the Add method instead of the DateTime addition operator.

[JScript] In JScript, you can use the operators defined by a type, but you cannot define your own.

Arguments [Visual Basic, JScript]

d
A DateTime.
t
A TimeSpan.

Parameters [C#, C++]

d
A DateTime.
t
A TimeSpan.

Return Value

A DateTime that is the sum of the values of d and t.

Exceptions

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

Example

[Visual Basic, C#, C++] The following sample demonstrates the addition operator.

[Visual Basic] 
Dim dTime As New System.DateTime(1980, 8, 5)

' tSpan is 17 days, 4 hours, 2 minutes and 1 second.
Dim tSpan As New System.TimeSpan(17, 4, 2, 1)

Dim result As System.DateTime

' Result gets 8/22/1980 4:02:01 AM.
result = System.DateTime.op_Addition(dTime, tSpan)

[C#] 
System.DateTime dTime = new System.DateTime(1980, 8, 5);

// tSpan is 17 days, 4 hours, 2 minutes and 1 second.
System.TimeSpan tSpan 
    = new System.TimeSpan(17, 4, 2, 1); 

// Result gets 8/22/1980 4:02:01 AM.
System.DateTime result = dTime + tSpan;

[C++] 
System::DateTime dTime(1980, 8, 5);

// tSpan is 17 days, 4 hours, 2 minutes and 1 second.
System::TimeSpan tSpan(17, 4, 2, 1);

// Result gets 8/22/1980 4:02:01 AM.
System::DateTime result = dTime + tSpan;

[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 | Ticks

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.