TimeSpan.Parse メソッドとは? わかりやすく解説

TimeSpan.Parse メソッド

文字列指定した時間間隔から、新しい TimeSpan オブジェクト作成します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function Parse
 ( _
    s As String _
) As TimeSpan
Dim s As String
Dim returnValue As TimeSpan

returnValue = TimeSpan.Parse(s)
public static TimeSpan Parse (
    string s
)
public:
static TimeSpan Parse (
    String^ s
)
public static TimeSpan Parse (
    String s
)
public static function Parse
 (
    s : String
) : TimeSpan

パラメータ

s

時間間隔指定する文字列

戻り値
s対応する TimeSpan

例外例外
例外種類条件

ArgumentNullException

snull 参照 (Visual Basic では Nothing) です。

FormatException

s形式無効です。

OverflowException

s が MinValue 未満数値か、MaxValue より大きい数値表してます。

または

日、時間、分、秒の各構成要素のうち、少なくとも 1 つが有効値の範囲にありません。

解説解説

s パラメータでは、次の形式時間間隔指定します

[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]

角かっこ ([および]) で囲まれている項目は省略可能です。中かっこ ({および}) で囲まれ縦棒 (|) で区切られた項目は選択肢リストからいずれか選択する必要がありますコロン (:) とピリオド (.) は必須のリテラル文字です。その他の項目次のとおりです。

項目

説明

ws

省略可能な空白

"-"

負の TimeSpan を示す省略可能なマイナス記号

d

日数 (0 ~ 10,675,199)。

hh

時間数 (0 ~ 23)。

mm

分数 (0 ~ 59)。

ss

省略可能な秒数 (0 ~ 59)。

ff

省略可能な秒の端数 (小数点以下 1 ~ 7 )。

sすべての構成要素には、MinValue上で MaxValue 以下の時間間隔指定する必要があります

使用例使用例

Parse メソッド使用して有効な TimeSpan 文字列からは 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.
// 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.
*/ 
// Example of the TimeSpan::Parse( String* ) and TimeSpan::ToString(
 ) 
// methods.
using namespace System;
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 = String::Concat( intervalToStr, "        " );
      Console::WriteLine( "{0,21}", intervalToStr );
   }
   catch ( Exception^ ex ) 
   {
      
      // If Parse throws an exception, write the message.
      Console::WriteLine( ex->Message );
   }

}

int 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.
*/
// Example of the TimeSpan.Parse( string ) and TimeSpan.ToString( )
 
// methods.
import 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 (System.Exception ex) {
            // If Parse throws an exception, write the message.
            Console.WriteLine(ex.get_Message());
        }
    } //ParseNDisplayTimeSpan

    public static void main(String[]
 args)
    {
        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");
    } //main
} //TSParseToStringDemo

/*
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.
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「TimeSpan.Parse メソッド」の関連用語

TimeSpan.Parse メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



TimeSpan.Parse メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS