C# DateTime Subtract(TimeSpan)

Description

DateTime Subtract(TimeSpan) subtracts the specified duration from this instance.

Syntax

DateTime.Subtract(TimeSpan) has the following syntax.


public DateTime Subtract(
  TimeSpan value
)

Parameters

DateTime.Subtract(TimeSpan) has the following parameters.

  • value - The time interval to subtract.

Returns

DateTime.Subtract(TimeSpan) method returns An object that is equal to the date and time represented by this instance minus the time interval represented by value.

Example

The following example demonstrates the Subtract method and the subtraction operator.


using System;//from   www.ja  v a2s  . com
public class MainClass{
  public static void Main(String[] argv){  
            System.DateTime date1 = new System.DateTime(2014, 6, 3, 22, 15, 0);
      System.DateTime date2 = new System.DateTime(2014, 12, 6, 13, 2, 0);
      System.DateTime date3 = new System.DateTime(2014, 10, 12, 8, 42, 0);

      // diff1 gets 185 days, 14 hours, and 47 minutes.
      System.TimeSpan diff1 = date2.Subtract(date1);

      // date4 gets 4/9/2014 5:55:00 PM.
      System.DateTime date4 = date3.Subtract(diff1);

      // diff2 gets 55 days 4 hours and 20 minutes.
      System.TimeSpan diff2 = date2 - date3;

      // date5 gets 4/9/2014 5:55:00 PM.
      System.DateTime date5 = date1 - diff2;

  }
}
    

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