Subtraction Operator

TimeSpan.Subtraction Operator

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Subtracts a specified TimeSpan from another specified TimeSpan.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Operator - ( _
	t1 As TimeSpan, _
	t2 As TimeSpan _
) As TimeSpan

Parameters

t1
Type: System.TimeSpan
The time interval to subtract from (the minuend).
t2
Type: System.TimeSpan
The time interval to subtract (the subtrahend).

Return Value

Type: System.TimeSpan
An object whose value is the result of the value of t1 minus the value of t2.

ExceptionCondition
OverflowException

The return value is less than MinValue or greater than MaxValue.

The following code example creates several pairs of TimeSpan objects and calculates their difference with the Subtraction operator.


' Example of the TimeSpan Addition and Subtraction operators.

Module Example

   Const dataFmt As String = "{0,-38}{1,24}"

   ' Pad the end of a TimeSpan string with spaces if it does not 
   ' contain milliseconds.
   Function Align(ByVal interval As TimeSpan) As String

      Dim intervalStr As String = interval.ToString()
      Dim pointIndex As Integer = intervalStr.IndexOf(":"c)

      pointIndex = intervalStr.IndexOf("."c, pointIndex)
      If pointIndex < 0 Then intervalStr &= "        "
      Align = intervalStr
   End Function

   ' Display TimeSpan parameters and their sum and difference.
   Sub ShowTimeSpanSumDiff(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal Left As TimeSpan, ByVal Right As TimeSpan)

      outputBlock.Text &= vbCrLf
      outputBlock.Text &= String.Format(dataFmt, "TimeSpan Left", Align(Left)) & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, "TimeSpan Right", Align(Right)) & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, _
          "TimeSpan.op_Addition( Left, Right )", _
          Align(TimeSpan.op_Addition(Left, Right))) & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, _
          "TimeSpan.op_Subtraction( Left, Right )", _
          Align(TimeSpan.op_Subtraction(Left, Right))) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text &= _
          "This example of the TimeSpan Addition and " & _
          "Subtraction " & vbCrLf & "operators " & _
          "generates the following output by creating " & vbCrLf & _
          "several pairs of TimeSpan objects and calculating " & _
          "and " & vbCrLf & "displaying the sum " & _
          "and difference of each." & vbCrLf

      ' Create pairs of TimeSpan objects.
      ShowTimeSpanSumDiff(outputBlock, _
          New TimeSpan(1, 20, 0), _
          New TimeSpan(0, 45, 10))
      ShowTimeSpanSumDiff(outputBlock,  _
          New TimeSpan(1, 10, 20, 30, 40), _
          New TimeSpan(-1, 2, 3, 4, 5))
      ShowTimeSpanSumDiff(outputBlock,  _
          New TimeSpan(182, 12, 30, 30, 505), _
          New TimeSpan(182, 11, 29, 29, 495))
      ShowTimeSpanSumDiff(outputBlock,  _
          New TimeSpan(888888888888888), _
          New TimeSpan(999999999999999))
   End Sub
End Module

' This example of the TimeSpan Addition and Subtraction
' operators generates the following output by creating
' several pairs of TimeSpan objects and calculating and
' displaying the sum and difference of each.
' 
' TimeSpan Left                                 01:20:00
' TimeSpan Right                                00:45:10
' TimeSpan.op_Addition( Left, Right )           02:05:10
' TimeSpan.op_Subtraction( Left, Right )        00:34:50
' 
' TimeSpan Left                               1.10:20:30.0400000
' TimeSpan Right                               -21:56:55.9950000
' TimeSpan.op_Addition( Left, Right )           12:23:34.0450000
' TimeSpan.op_Subtraction( Left, Right )      2.08:17:26.0350000
' 
' TimeSpan Left                             182.12:30:30.5050000
' TimeSpan Right                            182.11:29:29.4950000
' TimeSpan.op_Addition( Left, Right )       365.00:00:00
' TimeSpan.op_Subtraction( Left, Right )        01:01:01.0100000
' 
' TimeSpan Left                            1028.19:21:28.8888888
' TimeSpan Right                           1157.09:46:39.9999999
' TimeSpan.op_Addition( Left, Right )      2186.05:08:08.8888887
' TimeSpan.op_Subtraction( Left, Right )   -128.14:25:11.1111111


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft