C# DateTime Addition

Description

DateTime Addition adds a specified time interval to a specified date and time, yielding a new date and time.

Syntax

DateTime.Addition has the following syntax.


public static DateTime operator +(
  DateTime d,
  TimeSpan t
)

Parameters

DateTime.Addition has the following parameters.

  • d - The date and time value to add.
  • t - The time interval to add.

Returns

DateTime.Addition method returns An object that is the sum of the values of d and t.

Example

The following example demonstrates the addition operator.


using System;/* w  w w .  j ava2 s.c  o m*/
public class MainClass{
  public static void Main(String[] argv){  
    
            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;

            System.Console.WriteLine(result);
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version