TimeSpan.ParseExact メソッド

定義

時間間隔の文字列形式を等価の TimeSpan に変換します。 文字列形式の形式は、指定した形式と正確に一致する必要があります。

オーバーロード

ParseExact(String, String, IFormatProvider)

指定した書式およびカルチャ固有の書式情報を使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。 文字列形式の形式は、指定した形式と正確に一致する必要があります。

ParseExact(String, String[], IFormatProvider)

指定した書式指定文字列の配列とカルチャ固有の書式情報を使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。 文字列形式の形式は、指定した形式のいずれかと正確に一致する必要があります。

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles)

指定した書式およびカルチャ固有の書式情報を使用して、時間間隔の文字範囲を等価の TimeSpan に変換します。 文字列形式の形式は、指定した形式と正確に一致する必要があります。

ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpanStyles)

指定した書式、カルチャ固有の書式情報、およびスタイルを使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。 文字列形式の形式は、指定した形式のいずれかと正確に一致する必要があります。

ParseExact(String, String, IFormatProvider, TimeSpanStyles)

指定した書式、カルチャ固有の書式情報、およびスタイルを使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。 文字列形式の形式は、指定した形式と正確に一致する必要があります。

ParseExact(String, String[], IFormatProvider, TimeSpanStyles)

指定した書式、カルチャ固有の書式情報、およびスタイルを使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。 文字列形式の形式は、指定した形式のいずれかと正確に一致する必要があります。

ParseExact(String, String, IFormatProvider)

ソース:
TimeSpan.cs
ソース:
TimeSpan.cs
ソース:
TimeSpan.cs

指定した書式およびカルチャ固有の書式情報を使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。 文字列形式の形式は、指定した形式と正確に一致する必要があります。

public:
 static TimeSpan ParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider);
public static TimeSpan ParseExact (string input, string format, IFormatProvider formatProvider);
public static TimeSpan ParseExact (string input, string format, IFormatProvider? formatProvider);
static member ParseExact : string * string * IFormatProvider -> TimeSpan
Public Shared Function ParseExact (input As String, format As String, formatProvider As IFormatProvider) As TimeSpan

パラメーター

input
String

変換する時間間隔を指定する文字列。

format
String

inputの必要な形式を定義する標準書式指定文字列またはカスタム書式指定文字列。

formatProvider
IFormatProvider

カルチャ固有の書式設定情報を提供するオブジェクト。

戻り値

format および formatProviderで指定された inputに対応する時間間隔。

例外

inputnullです。

input 形式が無効です。

は、TimeSpan.MinValue より小さいか、TimeSpan.MaxValueより大きい数値を表します。

-又は-

input の日、時間、分、または秒のコンポーネントの少なくとも 1 つが有効な範囲外です。

次の例では、ParseExact(String, String, IFormatProvider) メソッドを使用して、さまざまな書式指定文字列とカルチャを使用して、時間間隔のいくつかの文字列表現を解析します。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string intervalString, format;
      TimeSpan interval;
      CultureInfo culture;
      
      // Parse hour:minute value with "g" specifier current culture.
      intervalString = "17:14";
      format = "g";
      culture = CultureInfo.CurrentCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", 
                           intervalString, format);
      }                     
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      }      
      
      // Parse hour:minute:second value with "G" specifier.
      intervalString = "17:14:48";
      format = "G";
      culture = CultureInfo.InvariantCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse hours:minute.second value with "G" specifier 
      // and current (en-US) culture.     
      intervalString = "17:14:48.153";
      format = "G";
      culture = CultureInfo.CurrentCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse days:hours:minute.second value with "G" specifier 
      // and current (en-US) culture.     
      intervalString = "3:17:14:48.153";
      format = "G";
      culture = CultureInfo.CurrentCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
            
      // Parse days:hours:minute.second value with "G" specifier 
      // and fr-FR culture.     
      intervalString = "3:17:14:48.153";
      format = "G";
      culture = new CultureInfo("fr-FR");
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse days:hours:minute.second value with "G" specifier 
      // and fr-FR culture.     
      intervalString = "3:17:14:48,153";
      format = "G";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse a single number using the "c" standard format string. 
      intervalString = "12";
      format = "c";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, null);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse a single number using the "%h" custom format string. 
      format = "%h";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, null);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse a single number using the "%s" custom format string. 
      format = "%s";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, null);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      }
   }
}
// The example displays the following output:
//       '17:14' --> 17:14:00
//       '17:14:48': Bad Format for 'G'
//       '17:14:48.153': Bad Format for 'G'
//       '3:17:14:48.153' --> 3.17:14:48.1530000
//       '3:17:14:48.153': Bad Format for 'G'
//       '3:17:14:48,153' --> 3.17:14:48.1530000
//       '12' --> 12.00:00:00
//       '12' --> 12:00:00
//       '12' --> 00:00:12
open System
open System.Globalization

do
    // Parse hour:minute value with "g" specifier current culture.
    let intervalString = "17:14"
    let format = "g"
    let culture = CultureInfo.CurrentCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse hour:minute:second value with "G" specifier.
    let intervalString = "17:14:48"
    let format = "G"
    let culture = CultureInfo.InvariantCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture)
        printfn $"'{intervalString}' --> {interval}"
    with
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse hours:minute.second value with "G" specifier 
    // and current (en-US) culture.     
    let intervalString = "17:14:48.153"
    let format = "G"
    let culture = CultureInfo.CurrentCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"

    // Parse days:hours:minute.second value with "G" specifier 
    // and current (en-US) culture.     
    let intervalString = "3:17:14:48.153"
    let format = "G"
    let culture = CultureInfo.CurrentCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
        
    // Parse days:hours:minute.second value with "G" specifier 
    // and fr-FR culture.     
    let intervalString = "3:17:14:48.153"
    let format = "G"
    let culture = CultureInfo "fr-FR"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse days:hours:minute.second value with "G" specifier 
    // and fr-FR culture.     
    let intervalString = "3:17:14:48,153"
    let format = "G"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"

    // Parse a single number using the "c" standard format string. 
    let intervalString = "12"
    let format = "c"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, null)
        printfn $"'{intervalString}' --> {interval}"
    with
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse a single number using the "%h" custom format string. 
    let format = "%h"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, null)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse a single number using the "%s" custom format string. 
    let format = "%s"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, null)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
// The example displays the following output:
//       '17:14' --> 17:14:00
//       '17:14:48': Bad Format for 'G'
//       '17:14:48.153': Bad Format for 'G'
//       '3:17:14:48.153' --> 3.17:14:48.1530000
//       '3:17:14:48.153': Bad Format for 'G'
//       '3:17:14:48,153' --> 3.17:14:48.1530000
//       '12' --> 12.00:00:00
//       '12' --> 12:00:00
//       '12' --> 00:00:12
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim intervalString, format As String
      Dim interval As TimeSpan
      Dim culture As CultureInfo
      
      ' Parse hour:minute value with "g" specifier current culture.
      intervalString = "17:14"
      format = "g"
      culture = CultureInfo.CurrentCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try      
      
      ' Parse hour:minute:second value with "G" specifier.
      intervalString = "17:14:48"
      format = "G"
      culture = CultureInfo.InvariantCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse hours:minute.second value with "G" specifier 
      ' and current (en-US) culture.     
      intervalString = "17:14:48.153"
      format = "G"
      culture = CultureInfo.CurrentCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 

      ' Parse days:hours:minute.second value with "G" specifier 
      ' and current (en-US) culture.     
      intervalString = "3:17:14:48.153"
      format = "G"
      culture = CultureInfo.CurrentCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
            
      ' Parse days:hours:minute.second value with "G" specifier 
      ' and fr-FR culture.     
      intervalString = "3:17:14:48.153"
      format = "G"
      culture = New CultureInfo("fr-FR")
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse days:hours:minute.second value with "G" specifier 
      ' and fr-FR culture.     
      intervalString = "3:17:14:48,153"
      format = "G"
      culture = New CultureInfo("fr-FR")
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 

      ' Parse a single number using the "c" standard format string. 
      intervalString = "12"
      format = "c"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, Nothing)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse a single number using the "%h" custom format string. 
      format = "%h"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, Nothing)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse a single number using the "%s" custom format string. 
      format = "%s"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, Nothing)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
   End Sub
End Module
' The example displays the following output:
'       '17:14' --> 17:14:00
'       '17:14:48': Bad Format for 'G'
'       '17:14:48.153': Bad Format for 'G'
'       '3:17:14:48.153' --> 3.17:14:48.1530000
'       '3:17:14:48.153': Bad Format for 'G'
'       '3:17:14:48,153' --> 3.17:14:48.1530000
'       '12' --> 12.00:00:00
'       '12' --> 12:00:00
'       '12' --> 00:00:12

注釈

ParseExact(String, String, IFormatProvider) メソッドは、時間間隔の文字列形式を解析します。これは、先頭と末尾の空白文字が無視されることを除き、format パラメーターで定義された形式にする必要があります。 inputformat の形式に正確に準拠している必要があるため、ユーザーによる文字列入力を時間間隔に変換するときは、常に例外処理を使用する必要があります。 例外処理を使用しない場合は、代わりに TryParseExact(String, String, IFormatProvider, TimeSpan) メソッドを呼び出すことができます。

format パラメーターは、1 つの標準書式指定子、または必要な形式の inputを定義する 1 つ以上のカスタム書式指定子を含む文字列です。 有効な書式指定文字列の詳細については、「標準の TimeSpan 書式指定文字列の とカスタム TimeSpan 書式指定文字列を参照してください。

大事な

ParseExact メソッドは、format が値が "g" または "G" である標準の TimeSpan 書式指定文字列である場合にのみ、formatProvider パラメーターで指定されたカルチャの規則を使用します。 "c"、"t"、および "T" 標準書式指定文字列では、インバリアント カルチャの書式設定規則が使用されます。 カスタム書式指定文字列は、入力文字列の正確な形式を定義し、リテラル文字を使用して時間間隔の構成要素を区切ります。

formatProvider パラメーターは、format が標準書式指定文字列の場合に、返される文字列の形式に関するカルチャ固有の情報を提供する IFormatProvider 実装です。 formatProvider パラメーターには、次のいずれかを指定できます。

formatProvidernullされている場合は、現在のカルチャに関連付けられている DateTimeFormatInfo オブジェクトが使用されます。

こちらもご覧ください

  • 標準の TimeSpan 書式指定文字列 を する
  • カスタム TimeSpan 書式指定文字列 を する

適用対象

ParseExact(String, String[], IFormatProvider)

ソース:
TimeSpan.cs
ソース:
TimeSpan.cs
ソース:
TimeSpan.cs

指定した書式指定文字列の配列とカルチャ固有の書式情報を使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。 文字列形式の形式は、指定した形式のいずれかと正確に一致する必要があります。

public:
 static TimeSpan ParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider formatProvider);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider? formatProvider);
static member ParseExact : string * string[] * IFormatProvider -> TimeSpan
Public Shared Function ParseExact (input As String, formats As String(), formatProvider As IFormatProvider) As TimeSpan

パラメーター

input
String

変換する時間間隔を指定する文字列。

formats
String[]

inputの必要な形式を定義する標準またはカスタム書式指定文字列の配列。

formatProvider
IFormatProvider

カルチャ固有の書式設定情報を提供するオブジェクト。

戻り値

formats および formatProviderで指定された inputに対応する時間間隔。

例外

inputnullです。

input 形式が無効です。

は、TimeSpan.MinValue より小さいか、TimeSpan.MaxValueより大きい数値を表します。

-又は-

input の日、時間、分、または秒のコンポーネントの少なくとも 1 つが有効な範囲外です。

次の例では、ParseExact(String, String[], IFormatProvider) メソッドを呼び出して、文字列配列の各要素を TimeSpan 値に変換します。 この例では、フランス語 - フランス語 ("fr-FR") カルチャの書式設定規則を使用して文字列を解釈します。 文字列は、一般的な短い形式または一般的な長い形式で時間間隔を表すことができます。

さらに、この例では、時間間隔解析メソッドが 1 桁を解釈する方法を変更します。 通常、1 桁は時間間隔の日数として解釈されます。 代わりに、%h カスタム書式指定文字列を使用して、1 桁を時間数として解釈します。 この変更を有効にするには、%h カスタム書式指定文字列の前に、formats 配列内の他の書式指定文字列が必要であることに注意してください。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] inputs = { "3", "16:42", "1:6:52:35.0625", 
                          "1:6:52:35,0625" }; 
      string[] formats = { "g", "G", "%h"};
      TimeSpan interval;
      CultureInfo culture = new CultureInfo("fr-FR");
      
      // Parse each string in inputs using formats and the fr-FR culture.
      foreach (string input in inputs) {
         try {
            interval = TimeSpan.ParseExact(input, formats, culture);
            Console.WriteLine("{0} --> {1:c}", input, interval);
         }
         catch (FormatException) {
            Console.WriteLine("{0} --> Bad Format", input);
         }      
         catch (OverflowException) {
            Console.WriteLine("{0} --> Overflow", input);   
         }            
      }
   }
}
// The example displays the following output:
//       3 --> 03:00:00
//       16:42 --> 16:42:00
//       1:6:52:35.0625 --> Bad Format
//       1:6:52:35,0625 --> 1.06:52:35.0625000
open System
open System.Globalization

let inputs = [| "3"; "16:42"; "1:6:52:35.0625"; "1:6:52:35,0625" |] 
let formats = [| "g"; "G"; "%h" |]
let culture = CultureInfo "fr-FR"

// Parse each string in inputs using formats and the fr-FR culture.
for input in inputs do
    try
        let interval = TimeSpan.ParseExact(input, formats, culture)
        printfn $"{input} --> {interval:c}"
    with
    | :? FormatException ->
        printfn $"{input} --> Bad Format"
    | :? OverflowException ->
        printfn $"{input} --> Overflow"
// The example displays the following output:
//       3 --> 03:00:00
//       16:42 --> 16:42:00
//       1:6:52:35.0625 --> Bad Format
//       1:6:52:35,0625 --> 1.06:52:35.0625000
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim inputs() As String = { "3", "16:42", "1:6:52:35.0625", 
                                 "1:6:52:35,0625" } 
      Dim formats() As String = { "%h", "g", "G" }
      Dim interval As TimeSpan
      Dim culture As New CultureInfo("fr-FR")
      
      ' Parse each string in inputs using formats and the fr-FR culture.
      For Each input As String In inputs
         Try
            interval = TimeSpan.ParseExact(input, formats, culture)
            Console.WriteLine("{0} --> {1:c}", input, interval)   
         Catch e As FormatException
            Console.WriteLine("{0} --> Bad Format", input)   
         Catch e As OverflowException
            Console.WriteLine("{0} --> Overflow", input)   
         End Try            
      Next
   End Sub
End Module
' The example displays the following output:
'       3 --> 3.00:00:00
'       16:42 --> 16:42:00
'       1:6:52:35.0625 --> Bad Format
'       1:6:52:35,0625 --> 1.06:52:35.0625000

注釈

ParseExact(String, String, IFormatProvider) メソッドは、時間間隔の文字列形式を解析します。これは、先頭と末尾の空白文字が無視されることを除き、formats パラメーターで定義された形式のいずれかで指定する必要があります。 input formatsで指定された形式のいずれかに正確に準拠している必要があるため、ユーザーによる文字列入力を時間間隔に変換するときは、常に例外処理を使用する必要があります。 例外処理を使用しない場合は、代わりに TryParseExact(String, String[], IFormatProvider, TimeSpan) メソッドを呼び出すことができます。

formats パラメーターは、1 つの標準書式指定子、または必要な形式の inputを定義する 1 つ以上のカスタム書式指定子で構成される要素を持つ文字列配列です。 有効な書式指定文字列の詳細については、「標準の TimeSpan 書式指定文字列の とカスタム TimeSpan 書式指定文字列を参照してください。 input 解析操作を成功させるには、formats のメンバーに正確に対応する必要があります。 解析操作は、配列内の最初の要素から始まる formats 内の各要素に input を照合しようとします。

大事な

ParseExact メソッドは、formatProvider パラメーターで指定されたカルチャの規則を使用します。これは、input の解析に使用される書式指定文字列が、値が "g" または "G" である標準の TimeSpan 書式指定文字列である場合のみです。 "c"、"t"、および "T" 標準書式指定文字列では、インバリアント カルチャの書式設定規則が使用されます。 カスタム書式指定文字列は、入力文字列の正確な形式を定義し、リテラル文字を使用して時間間隔の構成要素を区切ります。

formatProvider パラメーターは、input の解析に使用される書式指定文字列が標準の書式指定文字列である場合に、返される文字列の形式に関するカルチャ固有の情報を提供する IFormatProvider 実装です。 formatProvider パラメーターには、次のいずれかを指定できます。

formatProvidernullされている場合は、現在のカルチャに関連付けられている DateTimeFormatInfo オブジェクトが使用されます。

こちらもご覧ください

適用対象

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles)

ソース:
TimeSpan.cs
ソース:
TimeSpan.cs
ソース:
TimeSpan.cs

指定した書式およびカルチャ固有の書式情報を使用して、時間間隔の文字範囲を等価の TimeSpan に変換します。 文字列形式の形式は、指定した形式と正確に一致する必要があります。

public static TimeSpan ParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
public static TimeSpan ParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
static member ParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, Optional styles As TimeSpanStyles = System.Globalization.TimeSpanStyles.None) As TimeSpan

パラメーター

input
ReadOnlySpan<Char>

変換する時間間隔を指定するスパン。

format
ReadOnlySpan<Char>

inputの必要な形式を定義する標準書式指定文字列またはカスタム書式指定文字列。

formatProvider
IFormatProvider

カルチャ固有の書式設定情報を提供するオブジェクト。

styles
TimeSpanStyles

inputに存在する可能性があるスタイル要素を定義する列挙値のビットごとの組み合わせ。

戻り値

format および formatProviderで指定された inputに対応する時間間隔。

適用対象

ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpanStyles)

ソース:
TimeSpan.cs
ソース:
TimeSpan.cs
ソース:
TimeSpan.cs

指定した書式、カルチャ固有の書式情報、およびスタイルを使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。 文字列形式の形式は、指定した形式のいずれかと正確に一致する必要があります。

public static TimeSpan ParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
public static TimeSpan ParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
static member ParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As ReadOnlySpan(Of Char), formats As String(), formatProvider As IFormatProvider, Optional styles As TimeSpanStyles = System.Globalization.TimeSpanStyles.None) As TimeSpan

パラメーター

input
ReadOnlySpan<Char>

変換する時間間隔を指定するスパン。

formats
String[]

inputの必要な形式を定義する標準書式指定文字列またはカスタム書式指定文字列の配列。

formatProvider
IFormatProvider

カルチャ固有の書式設定情報を提供するオブジェクト。

styles
TimeSpanStyles

入力に存在する可能性があるスタイル要素を定義する列挙値のビットごとの組み合わせ。

戻り値

formatsformatProvider、および stylesで指定された inputに対応する時間間隔。

適用対象

ParseExact(String, String, IFormatProvider, TimeSpanStyles)

ソース:
TimeSpan.cs
ソース:
TimeSpan.cs
ソース:
TimeSpan.cs

指定した書式、カルチャ固有の書式情報、およびスタイルを使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。 文字列形式の形式は、指定した形式と正確に一致する必要があります。

public:
 static TimeSpan ParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string format, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string format, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles);
static member ParseExact : string * string * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As String, format As String, formatProvider As IFormatProvider, styles As TimeSpanStyles) As TimeSpan

パラメーター

input
String

変換する時間間隔を指定する文字列。

format
String

inputの必要な形式を定義する標準書式指定文字列またはカスタム書式指定文字列。

formatProvider
IFormatProvider

カルチャ固有の書式設定情報を提供するオブジェクト。

styles
TimeSpanStyles

inputに存在する可能性があるスタイル要素を定義する列挙値のビットごとの組み合わせ。

戻り値

formatformatProvider、および stylesで指定された inputに対応する時間間隔。

例外

styles は無効な TimeSpanStyles 値です。

inputnullです。

input 形式が無効です。

は、TimeSpan.MinValue より小さいか、TimeSpan.MaxValueより大きい数値を表します。

-又は-

input の日、時間、分、または秒のコンポーネントの少なくとも 1 つが有効な範囲外です。

次の例では、ParseExact(String, String, IFormatProvider) メソッドを使用して、さまざまな書式指定文字列とカルチャを使用して、時間間隔のいくつかの文字列表現を解析します。 また、TimeSpanStyles.AssumeNegative 値を使用して、各文字列を負の時間間隔として解釈します。 この例の出力は、カスタム書式指定文字列で使用されている場合にのみ、TimeSpanStyles.AssumeNegative スタイルが戻り値に影響することを示しています。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string intervalString, format;
      TimeSpan interval;
      CultureInfo culture = null;
      
      // Parse hour:minute value with custom format specifier.
      intervalString = "17:14";
      format = "h\\:mm";
      culture = CultureInfo.CurrentCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      }      
      
      // Parse hour:minute:second value with "g" specifier.
      intervalString = "17:14:48";
      format = "g";
      culture = CultureInfo.InvariantCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse hours:minute.second value with custom format specifier.     
      intervalString = "17:14:48.153";
      format = @"h\:mm\:ss\.fff";
      culture = null;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse days:hours:minute.second value with "G" specifier 
      // and current (en-US) culture.     
      intervalString = "3:17:14:48.153";
      format = "G";
      culture = CultureInfo.CurrentCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
            
      // Parse days:hours:minute.second value with a custom format specifier.     
      intervalString = "3:17:14:48.153";
      format = @"d\:hh\:mm\:ss\.fff";
      culture = null;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse days:hours:minute.second value with "G" specifier 
      // and fr-FR culture.     
      intervalString = "3:17:14:48,153";
      format = "G";
      culture = new CultureInfo("fr-FR");
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse a single number using the "c" standard format string. 
      intervalString = "12";
      format = "c";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        null, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse a single number using the "%h" custom format string. 
      format = "%h";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        null, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse a single number using the "%s" custom format string. 
      format = "%s";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        null, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
   }
}
// The example displays the following output:
//    '17:14' (h\:mm) --> -17:14:00
//    '17:14:48' (g) --> 17:14:48
//    '17:14:48.153' (h\:mm\:ss\.fff) --> -17:14:48.1530000
//    '3:17:14:48.153' (G) --> 3.17:14:48.1530000
//    '3:17:14:48.153' (d\:hh\:mm\:ss\.fff) --> -3.17:14:48.1530000
//    '3:17:14:48,153' (G) --> 3.17:14:48.1530000
//    '12' (c) --> 12.00:00:00
//    '12' (%h) --> -12:00:00
//    '12' (%s) --> -00:00:12
open System
open System.Globalization

do
    // Parse hour:minute value with custom format specifier.
    let intervalString = "17:14"
    let format = "h\\:mm"
    let culture = CultureInfo.CurrentCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse hour:minute:second value with "g" specifier.
    let intervalString = "17:14:48"
    let format = "g"
    let culture = CultureInfo.InvariantCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse hours:minute.second value with custom format specifier.     
    let intervalString = "17:14:48.153"
    let format = @"h\:mm\:ss\.fff"
    let culture = null
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"

    // Parse days:hours:minute.second value with "G" specifier 
    // and current (en-US) culture.     
    let intervalString = "3:17:14:48.153"
    let format = "G"
    let culture = CultureInfo.CurrentCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
        
    // Parse days:hours:minute.second value with a custom format specifier.     
    let intervalString = "3:17:14:48.153"
    let format = @"d\:hh\:mm\:ss\.fff"
    let culture = null
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse days:hours:minute.second value with "G" specifier 
    // and fr-FR culture.     
    let intervalString = "3:17:14:48,153"
    let format = "G"
    let culture = new CultureInfo("fr-FR")
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"

    // Parse a single number using the "c" standard format string. 
    let intervalString = "12"
    let format = "c"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, null, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse a single number using the "%h" custom format string. 
    let format = "%h"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, null, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse a single number using the "%s" custom format string. 
    let format = "%s"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, null, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
// The example displays the following output:
//    '17:14' (h\:mm) --> -17:14:00
//    '17:14:48' (g) --> 17:14:48
//    '17:14:48.153' (h\:mm\:ss\.fff) --> -17:14:48.1530000
//    '3:17:14:48.153' (G) --> 3.17:14:48.1530000
//    '3:17:14:48.153' (d\:hh\:mm\:ss\.fff) --> -3.17:14:48.1530000
//    '3:17:14:48,153' (G) --> 3.17:14:48.1530000
//    '12' (c) --> 12.00:00:00
//    '12' (%h) --> -12:00:00
//    '12' (%s) --> -00:00:12
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim intervalString, format As String
      Dim interval As TimeSpan
      Dim culture As CultureInfo = Nothing
      
      ' Parse hour:minute value with custom format specifier.
      intervalString = "17:14"
      format = "h\:mm"
      culture = CultureInfo.CurrentCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try      
      
      ' Parse hour:minute:second value with "g" specifier.
      intervalString = "17:14:48"
      format = "g"
      culture = CultureInfo.InvariantCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse hours:minute.second value with custom format specifier.     
      intervalString = "17:14:48.153"
      format = "h\:mm\:ss\.fff"
      culture = Nothing
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 

      ' Parse days:hours:minute.second value with "G" specifier 
      ' and current (en-US) culture.     
      intervalString = "3:17:14:48.153"
      format = "G"
      culture = CultureInfo.CurrentCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
            
      ' Parse days:hours:minute.second value with a custom format specifier.     
      intervalString = "3:17:14:48.153"
      format = "d\:hh\:mm\:ss\.fff"
      culture = Nothing
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse days:hours:minute.second value with "G" specifier 
      ' and fr-FR culture.     
      intervalString = "3:17:14:48,153"
      format = "G"
      culture = New CultureInfo("fr-FR")
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 

      ' Parse a single number using the "c" standard format string. 
      intervalString = "12"
      format = "c"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        Nothing, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse a single number using the "%h" custom format string. 
      format = "%h"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        Nothing, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse a single number using the "%s" custom format string. 
      format = "%s"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        Nothing, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
   End Sub
End Module
' The example displays the following output:
'    '17:14' (h\:mm) --> -17:14:00
'    '17:14:48' (g) --> 17:14:48
'    '17:14:48.153' (h\:mm\:ss\.fff) --> -17:14:48.1530000
'    '3:17:14:48.153' (G) --> 3.17:14:48.1530000
'    '3:17:14:48.153' (d\:hh\:mm\:ss\.fff) --> -3.17:14:48.1530000
'    '3:17:14:48,153' (G) --> 3.17:14:48.1530000
'    '12' (c) --> 12.00:00:00
'    '12' (%h) --> -12:00:00
'    '12' (%s) --> -00:00:12

注釈

ParseExact メソッドは、時間間隔の文字列形式を解析します。これは、先頭と末尾の空白文字が無視されることを除き、format パラメーターで定義された形式にする必要があります。 inputformat の形式に正確に準拠している必要があるため、ユーザーによる文字列入力を時間間隔に変換するときは、常に例外処理を使用する必要があります。 例外処理を使用しない場合は、代わりに TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) メソッドを呼び出すことができます。

format パラメーターは、1 つの標準書式指定子、または必要な形式の inputを定義する 1 つ以上のカスタム書式指定子を含む文字列です。 有効な書式指定文字列の詳細については、「標準の TimeSpan 書式指定文字列の とカスタム TimeSpan 書式指定文字列を参照してください。

大事な

ParseExact メソッドは、format が値が "g" または "G" である標準の TimeSpan 書式指定文字列である場合にのみ、formatProvider パラメーターで指定されたカルチャの規則を使用します。 "c"、"t"、および "T" 標準書式指定文字列では、インバリアント カルチャの書式設定規則が使用されます。 カスタム書式指定文字列は、入力文字列の正確な形式を定義し、リテラル文字を使用して時間間隔の構成要素を区切ります。

formatProvider パラメーターは、format が標準書式指定文字列の場合に、返される文字列の形式に関するカルチャ固有の情報を提供する IFormatProvider 実装です。 formatProvider パラメーターには、次のいずれかを指定できます。

formatProvidernullされている場合は、現在のカルチャに関連付けられている DateTimeFormatInfo オブジェクトが使用されます。

styles パラメーターは、カスタム書式指定文字列を使用して解析される文字列の解釈に影響します。 負の符号 (TimeSpanStyles.None) が存在する場合にのみ、input が負の時間間隔として解釈されるかどうか、または常に負の時間間隔 (TimeSpanStyles.AssumeNegative) として解釈されるかどうかを判断します。 TimeSpanStyles.AssumeNegative を使用しない場合、負の時間間隔を正常に解析するには、format にリテラル負符号記号 ("\-" など) を含める必要があります。

こちらもご覧ください

  • 標準の TimeSpan 書式指定文字列 を する
  • カスタム TimeSpan 書式指定文字列 を する

適用対象

ParseExact(String, String[], IFormatProvider, TimeSpanStyles)

ソース:
TimeSpan.cs
ソース:
TimeSpan.cs
ソース:
TimeSpan.cs

指定した書式、カルチャ固有の書式情報、およびスタイルを使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。 文字列形式の形式は、指定した形式のいずれかと正確に一致する必要があります。

public:
 static TimeSpan ParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles);
static member ParseExact : string * string[] * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As String, formats As String(), formatProvider As IFormatProvider, styles As TimeSpanStyles) As TimeSpan

パラメーター

input
String

変換する時間間隔を指定する文字列。

formats
String[]

inputの必要な形式を定義する標準書式指定文字列またはカスタム書式指定文字列の配列。

formatProvider
IFormatProvider

カルチャ固有の書式設定情報を提供するオブジェクト。

styles
TimeSpanStyles

入力に存在する可能性があるスタイル要素を定義する列挙値のビットごとの組み合わせ。

戻り値

formatsformatProvider、および stylesで指定された inputに対応する時間間隔。

例外

styles は無効な TimeSpanStyles 値です。

inputnullです。

input 形式が無効です。

は、TimeSpan.MinValue より小さいか、TimeSpan.MaxValueより大きい数値を表します。

-又は-

input の日、時間、分、または秒のコンポーネントの少なくとも 1 つが有効な範囲外です。

次の例では、ParseExact(String, String[], IFormatProvider, TimeSpanStyles) メソッドを呼び出して、文字列配列の各要素を TimeSpan 値に変換します。 文字列は、一般的な短い形式または一般的な長い形式で時間間隔を表すことができます。

さらに、この例では、時間間隔解析メソッドが 1 桁を解釈する方法を変更します。 通常、1 桁は時間間隔の日数として解釈されます。 代わりに、%h カスタム書式指定文字列を使用して、1 桁を時間数として解釈します。 この変更を有効にするには、%h カスタム書式指定文字列の前に、formats 配列内の他の書式指定文字列が必要であることに注意してください。 また、出力から、メソッド呼び出しで指定された TimeSpanStyles.AssumeNegative フラグは、この書式指定子を使用して文字列を解析する場合にのみ使用されることに注意してください。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] inputs = { "3", "16:42", "1:6:52:35.0625", 
                          "1:6:52:35,0625" }; 
      string[] formats = { "%h", "g", "G" };
      TimeSpan interval;
      CultureInfo culture = new CultureInfo("de-DE");
      
      // Parse each string in inputs using formats and the de-DE culture.
      foreach (string input in inputs) {
         try {
            interval = TimeSpan.ParseExact(input, formats, culture,
                                           TimeSpanStyles.AssumeNegative);
            Console.WriteLine("{0} --> {1:c}", input, interval);
         }
         catch (FormatException) {
            Console.WriteLine("{0} --> Bad Format", input);
         }      
         catch (OverflowException) {
            Console.WriteLine("{0} --> Overflow", input);   
         }            
      }
   }
}
// The example displays the following output:
//       3 --> -03:00:00
//       16:42 --> 16:42:00
//       1:6:52:35.0625 --> Bad Format
//       1:6:52:35,0625 --> 1.06:52:35.0625000
open System
open System.Globalization

let inputs = 
    [| "3"; "16:42"; "1:6:52:35.0625"; "1:6:52:35,0625" |] 
let formats = [| "%h"; "g"; "G" |]
let culture = CultureInfo "de-DE"

// Parse each string in inputs using formats and the de-DE culture.
for input in inputs do
    try
        let interval = 
            TimeSpan.ParseExact(input, formats, culture, TimeSpanStyles.AssumeNegative)
        printfn $"{input} --> {interval:c}"
    with
    | :? FormatException ->
        printfn $"{input} --> Bad Format"
    | :? OverflowException ->
        printfn $"{input} --> Overflow"
// The example displays the following output:
//       3 --> -03:00:00
//       16:42 --> 16:42:00
//       1:6:52:35.0625 --> Bad Format
//       1:6:52:35,0625 --> 1.06:52:35.0625000
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim inputs() As String = { "3", "16:42", "1:6:52:35.0625", 
                                 "1:6:52:35,0625" } 
      Dim formats() As String = { "%h", "g", "G" }
      Dim interval As TimeSpan
      Dim culture As New CultureInfo("de-DE")
      
      ' Parse each string in inputs using formats and the de-DE culture.
      For Each input As String In inputs
         Try
            interval = TimeSpan.ParseExact(input, formats, culture, 
                                           TimeSpanStyles.AssumeNegative)
            Console.WriteLine("{0} --> {1:c}", input, interval)   
         Catch e As FormatException
            Console.WriteLine("{0} --> Bad Format", input)   
         Catch e As OverflowException
            Console.WriteLine("{0} --> Overflow", input)   
         End Try            
      Next
   End Sub
End Module
' The example displays the following output:
'       3 --> -03:00:00
'       16:42 --> 16:42:00
'       1:6:52:35.0625 --> Bad Format
'       1:6:52:35,0625 --> 1.06:52:35.0625000

注釈

ParseExact(String, String[], IFormatProvider, TimeSpanStyles) メソッドは、時間間隔の文字列形式を解析します。これは、先頭と末尾の空白文字が無視されることを除き、formats パラメーターで定義された形式のいずれかで指定する必要があります。 input formatsで指定された形式のいずれかに正確に準拠している必要があるため、ユーザーによる文字列入力を時間間隔に変換するときは、常に例外処理を使用する必要があります。 例外処理を使用しない場合は、代わりに TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) メソッドを呼び出すことができます。

formats パラメーターは、1 つの標準書式指定子、または必要な形式の inputを定義する 1 つ以上のカスタム書式指定子で構成される要素を持つ文字列配列です。 有効な書式指定文字列の詳細については、「標準の TimeSpan 書式指定文字列の とカスタム TimeSpan 書式指定文字列を参照してください。 input 解析操作を成功させるには、formats のメンバーに正確に対応する必要があります。 解析操作は、配列内の最初の要素から始まる formats 内の各要素に input を照合しようとします。

大事な

ParseExact メソッドは、formatProvider パラメーターで指定されたカルチャの規則を使用します。これは、input の解析に使用される書式指定文字列が、値が "g" または "G" である標準の TimeSpan 書式指定文字列である場合のみです。 "c"、"t"、および "T" 標準書式指定文字列では、インバリアント カルチャの書式設定規則が使用されます。 カスタム書式指定文字列は、入力文字列の正確な形式を定義し、リテラル文字を使用して時間間隔の構成要素を区切ります。

formatProvider パラメーターは、input の解析に使用される書式指定文字列が標準の書式指定文字列である場合に、返される文字列の形式に関するカルチャ固有の情報を提供する IFormatProvider 実装です。 formatProvider パラメーターには、次のいずれかを指定できます。

formatProvidernullされている場合は、現在のカルチャに関連付けられている DateTimeFormatInfo オブジェクトが使用されます。

styles パラメーターは、カスタム書式指定文字列を使用して解析される文字列の解釈に影響します。 負の符号 (TimeSpanStyles.None) が存在する場合にのみ、input が負の時間間隔として解釈されるかどうか、または常に負の時間間隔 (TimeSpanStyles.AssumeNegative) として解釈されるかどうかを判断します。 TimeSpanStyles.AssumeNegative を使用しない場合、負の時間間隔を正常に解析するには、format にリテラル負符号記号 ("\-" など) を含める必要があります。

こちらもご覧ください

  • 標準の TimeSpan 書式指定文字列 を する
  • カスタム TimeSpan 書式指定文字列 を する

適用対象