C# DateTime Add

Description

DateTime Add returns a new DateTime that adds the value of the specified TimeSpan to the value of this instance.

Syntax

DateTime.Add has the following syntax.


public DateTime Add(
  TimeSpan value
)

Parameters

DateTime.Add has the following parameters.

  • value - A positive or negative time interval.

Returns

DateTime.Add 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.

Example

The following example demonstrates the Add method. It calculates the day of the week that is 36 days (864 hours) from this moment.


using System;/*  www  .j  ava2  s .  co m*/
public class MainClass{
  public static void Main(String[] argv){  

    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);

  }
}

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