Export (0) Print
Expand All

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)

public:
TimeSpan Duration()

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 TimeSpan::MinValue.

The following 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. 
using namespace System;
const __wchar_t * protoFmt = L"{0,22}{1,22}{2,22}";
void ShowDurationNegate( TimeSpan interval )
{

   // Display the TimeSpan value and the results of the  
   // Duration and Negate methods.
   Console::WriteLine( gcnew String( protoFmt ), interval, interval.Duration(), interval.Negate() );
}

int main()
{
   Console::WriteLine( "This example of TimeSpan::Duration( ), " 
   "TimeSpan::Negate( ), \nand the TimeSpan Unary " 
   "Negation and Unary Plus operators \n" 
   "generates the following output.\n" );
   Console::WriteLine( gcnew String( protoFmt ), "TimeSpan", "Duration( )", "Negate( )" );
   Console::WriteLine( gcnew String( protoFmt ), "--------", "-----------", "---------" );

   // Create TimeSpan objects and apply the Unary Negation 
   // and Unary Plus operators to them.
   ShowDurationNegate( TimeSpan(1) );
   ShowDurationNegate( TimeSpan( -1234567) );
   ShowDurationNegate(  +TimeSpan(0,0,10,-20,-30) );
   ShowDurationNegate(  +TimeSpan(0,-10,20,-30,40) );
   ShowDurationNegate(  -TimeSpan(1,10,20,40,160) );
   ShowDurationNegate(  -TimeSpan( -10,-20,-30,-40,-50) );
}

/*
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
*/

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft