次の方法で共有


TimeSpan.ToString メソッド

このインスタンスの値の文字列形式を返します。

Overrides Public Function ToString() As String
[C#]
public override string ToString();
[C++]
public: String* ToString();
[JScript]
public override function ToString() : String;

戻り値

このインスタンスの値を表す文字列。戻り値の形式は次のとおりです。

[-][d.]hh:mm:ss[.ff]

角かっこ ('[' および ']') で囲まれている項目は省略可能です。コロンとピリオド (':' および '.') はリテラル文字です。その他の項目は次のとおりです。

項目 説明
"-" 負の時間を示す、省略可能なマイナス記号。
"d" 省略可能な日数。
"hh" 時間数 (0 ~ 23)。
"mm" 分数 (0 ~ 59)。
"ss" 秒数 (0 ~ 59)。
"ff" 省略可能な秒の端数 (小数点以下 1 ~ 7 桁)。

メモ    TimeSpan の文字列形式と Oracle データ型を比較する方法の詳細については、https://support.microsoft.com にある Microsoft サポート技術情報の文書 Q324577 (「System.TimeSpan Does Not Match Oracle 9i INTERVAL DAY TO SECOND Data Type」) を参照してください。

解説

このメソッドの戻り値は、 Parse で使用できます。

使用例

[Visual Basic, C#, C++] ToString メソッドを使用して、 TimeSpan オブジェクトから TimeSpan 文字列を作成するコード例を次に示します。

 
' Example of the TimeSpan.Parse( String ) and TimeSpan.ToString( ) 
' methods.
Imports System
Imports Microsoft.VisualBasic

Module TSParseToStringDemo

    Sub ParseNDisplayTimeSpan( intervalStr As String )

        ' Write the first part of the output line.
        Console.Write( "{0,20}   ", intervalStr )

        ' Parse the parameter, and then convert it back to a string.
        Try
            Dim intervalVal As TimeSpan = TimeSpan.Parse( intervalStr )
            Dim intervalToStr As String = intervalVal.ToString( )

            ' Pad the end of the TimeSpan string with spaces if it 
            ' does not contain milliseconds.
            Dim pIndex As Integer = intervalToStr.IndexOf( ":"c )
            pIndex = intervalToStr.IndexOf( "."c, pIndex )
            If pIndex < 0 Then   intervalToStr &= "        "

            Console.WriteLine( "{0,21}", intervalToStr )

        ' If Parse throws an exception, write the message.
        Catch ex As Exception
            Console.WriteLine( ex.Message )
        End Try
    End Sub 

    Sub Main( )

        Console.WriteLine( _
            "This example of TimeSpan.Parse( String ) and " & _
            vbCrLf & "TimeSpan.ToString( ) " & _
            "generates the following output." & vbCrLf )
        Console.WriteLine( "{0,20}   {1,21}", _
            "String to Parse", "TimeSpan or Exception" )    
        Console.WriteLine( "{0,20}   {1,21}", _
            "---------------", "---------------------" )    

        ParseNDisplayTimeSpan( "0" )
        ParseNDisplayTimeSpan( "14" )
        ParseNDisplayTimeSpan( "1:2:3" )
        ParseNDisplayTimeSpan( "0:0:0.250" )
        ParseNDisplayTimeSpan( "10.20:30:40.50" )
        ParseNDisplayTimeSpan( "99.23:59:59.9999999" )
        ParseNDisplayTimeSpan( "0023:0059:0059.0099" )
        ParseNDisplayTimeSpan( "24:0:0" )
        ParseNDisplayTimeSpan( "0:60:0" )
        ParseNDisplayTimeSpan( "0:0:60" )
        ParseNDisplayTimeSpan( "10:" )
        ParseNDisplayTimeSpan( ":10" )
        ParseNDisplayTimeSpan( "10:20:" )
        ParseNDisplayTimeSpan( ".123" )
        ParseNDisplayTimeSpan( "10." )
        ParseNDisplayTimeSpan( "10.12" )
    End Sub 
End Module 

' This example of TimeSpan.Parse( String ) and
' TimeSpan.ToString( ) generates the following output.
' 
'      String to Parse   TimeSpan or Exception
'      ---------------   ---------------------
'                    0        00:00:00
'                   14     14.00:00:00
'                1:2:3        01:02:03
'            0:0:0.250        00:00:00.2500000
'       10.20:30:40.50     10.20:30:40.5000000
'  99.23:59:59.9999999     99.23:59:59.9999999
'  0023:0059:0059.0099        23:59:59.0099000
'               24:0:0   TimeSpan overflowed because the duration is too long.
'               0:60:0   TimeSpan overflowed because the duration is too long.
'               0:0:60   TimeSpan overflowed because the duration is too long.
'                  10:   Input string was not in a correct format.
'                  :10   Input string was not in a correct format.
'               10:20:   Input string was not in a correct format.
'                 .123   Input string was not in a correct format.
'                  10.   Input string was not in a correct format.
'                10.12   Input string was not in a correct format.

[C#] 
// Example of the TimeSpan.Parse( string ) and TimeSpan.ToString( ) 
// methods.
using System;

class TSParseToStringDemo
{
    static void ParseNDisplayTimeSpan( string intervalStr )
    {
        // Write the first part of the output line.
        Console.Write( "{0,20}   ", intervalStr );

        // Parse the parameter, and then convert it back to a string.
        try
        {
            TimeSpan intervalVal = TimeSpan.Parse( intervalStr );
            string   intervalToStr = intervalVal.ToString( );

            // Pad the end of the TimeSpan string with spaces if it 
            // does not contain milliseconds.
            int pIndex = intervalToStr.IndexOf( ':' );
            pIndex = intervalToStr.IndexOf( '.', pIndex );
            if( pIndex < 0 )   intervalToStr += "        ";

            Console.WriteLine( "{0,21}", intervalToStr );
        }
        catch( Exception ex )
        {
            // If Parse throws an exception, write the message.
            Console.WriteLine( ex.Message );
        }
    } 

    static void Main( )
    {
        Console.WriteLine(
            "This example of TimeSpan.Parse( string ) and \n" +
            "TimeSpan.ToString( ) " +
            "generates the following output.\n" );
        Console.WriteLine( "{0,20}   {1,21}", 
            "String to Parse", "TimeSpan or Exception" );
        Console.WriteLine( "{0,20}   {1,21}", 
            "---------------", "---------------------" );

        ParseNDisplayTimeSpan( "0" );
        ParseNDisplayTimeSpan( "14" );
        ParseNDisplayTimeSpan( "1:2:3" );
        ParseNDisplayTimeSpan( "0:0:0.250" );
        ParseNDisplayTimeSpan( "10.20:30:40.50" );
        ParseNDisplayTimeSpan( "99.23:59:59.9999999" );
        ParseNDisplayTimeSpan( "0023:0059:0059.0099" );
        ParseNDisplayTimeSpan( "24:0:0" );
        ParseNDisplayTimeSpan( "0:60:0" );
        ParseNDisplayTimeSpan( "0:0:60" );
        ParseNDisplayTimeSpan( "10:" );
        ParseNDisplayTimeSpan( ":10" );
        ParseNDisplayTimeSpan( "10:20:" );
        ParseNDisplayTimeSpan( ".123" );
        ParseNDisplayTimeSpan( "10." );
        ParseNDisplayTimeSpan( "10.12" );
    } 
} 

/*
This example of TimeSpan.Parse( string ) and
TimeSpan.ToString( ) generates the following output.

     String to Parse   TimeSpan or Exception
     ---------------   ---------------------
                   0        00:00:00
                  14     14.00:00:00
               1:2:3        01:02:03
           0:0:0.250        00:00:00.2500000
      10.20:30:40.50     10.20:30:40.5000000
 99.23:59:59.9999999     99.23:59:59.9999999
 0023:0059:0059.0099        23:59:59.0099000
              24:0:0   TimeSpan overflowed because the duration is too long.
              0:60:0   TimeSpan overflowed because the duration is too long.
              0:0:60   TimeSpan overflowed because the duration is too long.
                 10:   Input string was not in a correct format.
                 :10   Input string was not in a correct format.
              10:20:   Input string was not in a correct format.
                .123   Input string was not in a correct format.
                 10.   Input string was not in a correct format.
               10.12   Input string was not in a correct format.
*/ 

[C++] 
// Example of the TimeSpan::Parse( String* ) and TimeSpan::ToString( ) 
// methods.
#using <mscorlib.dll>
using namespace System;

void ParseNDisplayTimeSpan( String* intervalStr )
{
    // Write the first part of the output line.
    Console::Write( S"{0,20}   ", intervalStr );

    // Parse the parameter, and then convert it back to a string.
    try
    {
        TimeSpan intervalVal = TimeSpan::Parse( intervalStr );
        String*  intervalToStr = intervalVal.ToString( );

        // Pad the end of the TimeSpan string with spaces if it 
        // does not contain milliseconds.
        int pIndex = intervalToStr->IndexOf( ':' );
        pIndex = intervalToStr->IndexOf( '.', pIndex );
        if( pIndex < 0 )   intervalToStr = 
            String::Concat( intervalToStr, S"        " );

        Console::WriteLine( S"{0,21}", intervalToStr );
    }
    catch( Exception* ex )
    {
        // If Parse throws an exception, write the message.
        Console::WriteLine( ex->Message );
    }
} 

void main( )
{
    Console::WriteLine(
        S"This example of TimeSpan::Parse( String* ) and \n" 
        S"TimeSpan::ToString( ) " 
        S"generates the following output.\n" );
    Console::WriteLine( S"{0,20}   {1,21}", 
        S"String to Parse", S"TimeSpan or Exception" );
    Console::WriteLine( S"{0,20}   {1,21}", 
        S"---------------", S"---------------------" );

    ParseNDisplayTimeSpan( S"0" );
    ParseNDisplayTimeSpan( S"14" );
    ParseNDisplayTimeSpan( S"1:2:3" );
    ParseNDisplayTimeSpan( S"0:0:0.250" );
    ParseNDisplayTimeSpan( S"10.20:30:40.50" );
    ParseNDisplayTimeSpan( S"99.23:59:59.9999999" );
    ParseNDisplayTimeSpan( S"0023:0059:0059.0099" );
    ParseNDisplayTimeSpan( S"24:0:0" );
    ParseNDisplayTimeSpan( S"0:60:0" );
    ParseNDisplayTimeSpan( S"0:0:60" );
    ParseNDisplayTimeSpan( S"10:" );
    ParseNDisplayTimeSpan( S":10" );
    ParseNDisplayTimeSpan( S"10:20:" );
    ParseNDisplayTimeSpan( S".123" );
    ParseNDisplayTimeSpan( S"10." );
    ParseNDisplayTimeSpan( S"10.12" );
}

/*
This example of TimeSpan::Parse( String* ) and
TimeSpan::ToString( ) generates the following output.

     String to Parse   TimeSpan or Exception
     ---------------   ---------------------
                   0        00:00:00
                  14     14.00:00:00
               1:2:3        01:02:03
           0:0:0.250        00:00:00.2500000
      10.20:30:40.50     10.20:30:40.5000000
 99.23:59:59.9999999     99.23:59:59.9999999
 0023:0059:0059.0099        23:59:59.0099000
              24:0:0   TimeSpan overflowed because the duration is too long.
              0:60:0   TimeSpan overflowed because the duration is too long.
              0:0:60   TimeSpan overflowed because the duration is too long.
                 10:   Input string was not in a correct format.
                 :10   Input string was not in a correct format.
              10:20:   Input string was not in a correct format.
                .123   Input string was not in a correct format.
                 10.   Input string was not in a correct format.
               10.12   Input string was not in a correct format.
*/ 

[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 名前空間 | String | Parse