次の方法で共有


TimeSpan.Duration メソッド

このインスタンスの絶対値を値とする TimeSpan を返します。

Public Function Duration() As TimeSpan
[C#]
public TimeSpan Duration();
[C++]
public: TimeSpan Duration();
[JScript]
public function Duration() : TimeSpan;

戻り値

このインスタンスの値であり、必要に応じて正数に変換される値を持つ TimeSpan

例外

例外の種類 条件
OverflowException このインスタンスの値が、 MinValue です。

解説

TimeSpan の値は、それ自体が格納しているタイマ刻み数であり、正または負のどちらの場合もあります。

使用例

[Visual Basic, C#, C++] 複数の TimeSpan オブジェクトに Duration メソッドを適用するコード例を次に示します。

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

Module DuraNegaUnaryDemo

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

    Sub ShowDurationNegate( interval As TimeSpan )

        ' Display the TimeSpan value and the results of the 
        ' Duration and Negate methods.
        Console.WriteLine( dataFmt, _
            interval, interval.Duration( ), interval.Negate( ) )
    End Sub

    Sub Main( )

        Console.WriteLine( _
            "This example of TimeSpan.Duration( ), " & _
            "TimeSpan.Negate( ), " & vbCrLf & _
            "and the TimeSpan Unary Negation and " & _
            "Unary Plus operators " & vbCrLf & _
            "generates the following output." & vbCrLf )
        Console.WriteLine( dataFmt, _
            "TimeSpan", "Duration( )", "Negate( )" )    
        Console.WriteLine( dataFmt, _
            "--------", "-----------", "---------" )    

        ' Create TimeSpan objects and apply the Unary Negation
        ' and Unary Plus operators to them.
        ShowDurationNegate( new TimeSpan( 1 ) )
        ShowDurationNegate( new TimeSpan( -1234567 ) )
        ShowDurationNegate( TimeSpan.op_UnaryPlus( _
            new TimeSpan( 0, 0, 10, -20, -30 ) ) )
        ShowDurationNegate( TimeSpan.op_UnaryPlus( _
            new TimeSpan( 0, -10, 20, -30, 40 ) ) )
        ShowDurationNegate( TimeSpan.op_UnaryNegation( _
            new TimeSpan( 1, 10, 20, 40, 160 ) ) )
        ShowDurationNegate( 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

[C#] 
// Example of the TimeSpan.Duration( ) and TimeSpan.Negate( ) methods,
// and the TimeSpan Unary Negation and Unary Plus operators.
using System;

class DuraNegaUnaryDemo
{
    const string dataFmt = "{0,22}{1,22}{2,22}" ;

    static void ShowDurationNegate( TimeSpan interval )
    {
        // Display the TimeSpan value and the results of the 
        // Duration and Negate methods.
        Console.WriteLine( dataFmt, 
            interval, interval.Duration( ), interval.Negate( ) );
    }

    static void 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( dataFmt, 
            "TimeSpan", "Duration( )", "Negate( )" );
        Console.WriteLine( dataFmt, 
            "--------", "-----------", "---------" );

        // Create TimeSpan objects and apply the Unary Negation
        // and Unary Plus operators to them.
        ShowDurationNegate( new TimeSpan( 1 ) );
        ShowDurationNegate( new TimeSpan( -1234567 ) );
        ShowDurationNegate( 
            + new TimeSpan( 0, 0, 10, -20, -30 ) );
        ShowDurationNegate( 
            + new TimeSpan( 0, -10, 20, -30, 40 ) );
        ShowDurationNegate( 
            - new TimeSpan( 1, 10, 20, 40, 160 ) );
        ShowDurationNegate( 
            - new 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
*/ 

[C++] 
// Example of the TimeSpan::Duration( ) and TimeSpan::Negate( ) methods,
// and the TimeSpan Unary Negation and Unary Plus operators.
#using <mscorlib.dll>
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( new String( protoFmt ), 
        __box( interval ), __box( interval.Duration( ) ), 
        __box( interval.Negate( ) ) );
}

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

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

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

TimeSpan 構造体 | TimeSpan メンバ | System 名前空間 | Ticks