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

TimeSpan.Duration Method

Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
'Declaration
Public Function Duration As TimeSpan

Return Value

Type: System.TimeSpan
A new object whose value is the absolute value of the current TimeSpan object.
ExceptionCondition
OverflowException

The value of this instance is MinValue.

The following code example applies the Duration method to several TimeSpan objects.


' Example of the TimeSpan.Duration( ) and TimeSpan.Negate( ) methods,
' and the TimeSpan Unary Negation and Unary Plus operators.

Module Example

   Const dataFmt As String = "{0,22}{1,22}{2,22}"

   Sub ShowDurationNegate(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal interval As TimeSpan)

      ' Display the TimeSpan value and the results of the 
      ' Duration and Negate methods.
      outputBlock.Text &= String.Format(dataFmt, _
          interval, interval.Duration(), interval.Negate()) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text &= String.Format( _
          "This example of TimeSpan.Duration( ), " & _
          "TimeSpan.Negate( ), " & vbCrLf & _
          "and the TimeSpan Unary Negation and " & _
          "Unary Plus operators " & vbCrLf & _
          "generates the following output." & vbCrLf) & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, _
          "TimeSpan", "Duration( )", "Negate( )") & vbCrLf
      outputBlock.Text &= String.Format(dataFmt, _
          "--------", "-----------", "---------") & vbCrLf

      ' Create TimeSpan objects and apply the Unary Negation
      ' and Unary Plus operators to them.
      ShowDurationNegate(outputBlock, New TimeSpan(1))
      ShowDurationNegate(outputBlock, New TimeSpan(-1234567))
      ShowDurationNegate(outputBlock, TimeSpan.op_UnaryNegation( _
          New TimeSpan(1, 10, 20, 40, 160)))
      ShowDurationNegate(outputBlock, TimeSpan.op_UnaryNegation( _
          New TimeSpan(-10, -20, -30, -40, -50)))
   End Sub
End Module

' This example of TimeSpan.Duration( ), TimeSpan.Negate( ),
' and the TimeSpan Unary Negation and Unary Plus operators
' generates the following output.
' 
'               TimeSpan           Duration( )             Negate( )
'               --------           -----------             ---------
'       00:00:00.0000001      00:00:00.0000001     -00:00:00.0000001
'      -00:00:00.1234567      00:00:00.1234567      00:00:00.1234567
'       00:09:39.9700000      00:09:39.9700000     -00:09:39.9700000
'      -09:40:29.9600000      09:40:29.9600000      09:40:29.9600000
'    -1.10:20:40.1600000    1.10:20:40.1600000    1.10:20:40.1600000
'    10.20:30:40.0500000   10.20:30:40.0500000  -10.20:30:40.0500000


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.