Language

TimeSpan.Parse メソッド

定義

時間間隔の文字列形式を等価の TimeSpan に変換します。

オーバーロード

名前 説明
Parse(ReadOnlySpan<Char>, IFormatProvider)

指定したカルチャ固有の書式情報を使用して、時間間隔のスパン表現を同等の TimeSpan に変換します。

Parse(String, IFormatProvider)

指定したカルチャ固有の書式情報を使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。

Parse(String)

時間間隔の文字列形式を等価の TimeSpan に変換します。

Parse(ReadOnlySpan<Char>, IFormatProvider)

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

指定したカルチャ固有の書式情報を使用して、時間間隔のスパン表現を同等の TimeSpan に変換します。

public static TimeSpan Parse(ReadOnlySpan<char> input, IFormatProvider? formatProvider = default);
public static TimeSpan Parse(ReadOnlySpan<char> input, IFormatProvider formatProvider = default);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> TimeSpan
Public Shared Function Parse (input As ReadOnlySpan(Of Char), Optional formatProvider As IFormatProvider = Nothing) As TimeSpan

パラメーター

input
ReadOnlySpan<Char>

変換する時間間隔を表す文字を含むスパン。

formatProvider
IFormatProvider

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

返品

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

実装

適用対象

Parse(String, IFormatProvider)

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

指定したカルチャ固有の書式情報を使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。

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

パラメーター

input
String

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

formatProvider
IFormatProvider

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

返品

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

実装

例外

input は nullです。

input の形式が無効です。

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

-又は-

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

例

次の例では、 CultureInfo オブジェクトの配列を定義し、 Parse(String, IFormatProvider) メソッドの呼び出しで各オブジェクトを使用して、文字列配列内の要素を解析します。 この例では、特定のカルチャの規則が書式設定操作にどのように影響するかを示します。

using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class Example2
{
   public static void Main()
   {
      string[] values = { "6", "6:12", "6:12:14", "6:12:14:45",
                          "6.12:14:45", "6:12:14:45.3448",
                          "6:12:14:45,3448", "6:34:14:45" };
      CultureInfo[] cultures = { new CultureInfo("en-US"),
                                 new CultureInfo("ru-RU"),
                                 CultureInfo.InvariantCulture };

      string header = String.Format("{0,-17}", "String");
      foreach (CultureInfo culture in cultures)
         header += culture.Equals(CultureInfo.InvariantCulture) ?
                      String.Format("{0,20}", "Invariant") :
                      String.Format("{0,20}", culture.Name);
      Console.WriteLine(header);
      Console.WriteLine();

      foreach (string value in values)
      {
         Console.Write("{0,-17}", value);
         foreach (CultureInfo culture in cultures)
         {
            try {
               TimeSpan ts = TimeSpan.Parse(value, culture);
               Console.Write("{0,20}", ts.ToString("c"));
            }
            catch (FormatException) {
               Console.Write("{0,20}", "Bad Format");
            }
            catch (OverflowException) {
               Console.Write("{0,20}", "Overflow");
            }
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//    String                          en-US               ru-RU           Invariant
//
//    6                          6.00:00:00          6.00:00:00          6.00:00:00
//    6:12                         06:12:00            06:12:00            06:12:00
//    6:12:14                      06:12:14            06:12:14            06:12:14
//    6:12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
//    6.12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
//    6:12:14:45.3448    6.12:14:45.3448000          Bad Format  6.12:14:45.3448000
//    6:12:14:45,3448            Bad Format  6.12:14:45.3448000          Bad Format
//    6:34:14:45                   Overflow            Overflow            Overflow
open System
open System.Globalization
open System.Text.RegularExpressions

let values = 
    [| "6"; "6:12"; "6:12:14"; "6:12:14:45" 
       "6.12:14:45"; "6:12:14:45.3448"
       "6:12:14:45,3448"; "6:34:14:45" |]
let cultures = 
    [| CultureInfo "en-US" 
       CultureInfo "ru-RU"
       CultureInfo.InvariantCulture |]

let mutable header = $"""{"String",-17}"""
for culture in cultures do
    header <- header +
        if culture.Equals CultureInfo.InvariantCulture then 
            $"""{"Invariant",20}"""
        else
            $"{culture.Name,20}"
printfn $"{header}\m"

for value in values do
    printf $"{value,-17}"
    for culture in cultures do
        try
            let ts = TimeSpan.Parse(value, culture)
            printf $"{ts,20:c}"
        with
        | :? FormatException ->
            printf $"""{"Bad Format",20}"""
        | :? OverflowException ->
            printf $"""{"Overflow",20}"""
    printfn ""                      
// The example displays the following output:
//    String                          en-US               ru-RU           Invariant
//    
//    6                          6.00:00:00          6.00:00:00          6.00:00:00
//    6:12                         06:12:00            06:12:00            06:12:00
//    6:12:14                      06:12:14            06:12:14            06:12:14
//    6:12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
//    6.12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
//    6:12:14:45.3448    6.12:14:45.3448000          Bad Format  6.12:14:45.3448000
//    6:12:14:45,3448            Bad Format  6.12:14:45.3448000          Bad Format
//    6:34:14:45                   Overflow            Overflow            Overflow
Dim values() As String = {"6", "6:12", "6:12:14", "6:12:14:45",
                         "6.12:14:45", "6:12:14:45.3448",
                         "6:12:14:45,3448", "6:34:14:45"}
Dim cultures() As CultureInfo = {New CultureInfo("en-US"),
                                New CultureInfo("ru-RU"),
                                CultureInfo.InvariantCulture}

Dim header As String = String.Format("{0,-17}", "String")
For Each culture As CultureInfo In cultures
    header += If(culture.Equals(CultureInfo.InvariantCulture),
              String.Format("{0,20}", "Invariant"),
              String.Format("{0,20}", culture.Name))
Next
Console.WriteLine(header)
Console.WriteLine()

For Each value As String In values
    Console.Write("{0,-17}", value)
    For Each culture As CultureInfo In cultures
        Try
            Dim ts As TimeSpan = TimeSpan.Parse(value, culture)
            Console.Write("{0,20}", ts.ToString("c"))
        Catch e As FormatException
            Console.Write("{0,20}", "Bad Format")
        Catch e As OverflowException
            Console.Write("{0,20}", "Overflow")
        End Try
    Next
    Console.WriteLine()
Next

' The example displays the following output:
'    String                          en-US               ru-RU           Invariant
'    
'    6                          6.00:00:00          6.00:00:00          6.00:00:00
'    6:12                         06:12:00            06:12:00            06:12:00
'    6:12:14                      06:12:14            06:12:14            06:12:14
'    6:12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
'    6.12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
'    6:12:14:45.3448    6.12:14:45.3448000          Bad Format  6.12:14:45.3448000
'    6:12:14:45,3448            Bad Format  6.12:14:45.3448000          Bad Format
'    6:34:14:45                   Overflow            Overflow            Overflow

注釈

このメソッドは、inputで指定されたカルチャのカルチャ固有の各形式を使用して、formatProviderの解析を試みます。

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

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

Parse メソッドへの入力文字列には、次の形式の時間間隔指定が含まれています。

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

角かっこ ([ および ]) の要素は省略可能です。 中かっこ ({ と }) で囲み、垂直バー (|) で区切られた代替候補の一覧から 1 つ選択する必要があります。 次の表は、それぞれの要素の説明です。

要素 説明
ws 省略可能な空白。
- 負の TimeSpanを示す省略可能な負符号。
d 0 から10675199までの日数。
。 日と時間を区切る文化に配慮したシンボル。 インバリアント形式では、ピリオド (".") 文字が使用されます。
はあ 0 から 23 までの時間。
: カルチャに依存する時刻区切り記号。 インバリアント形式では、コロン (":") 文字が使用されます。
ミリメートル 分 (0 から 59 まで)。
ss 0 ~ 59 の範囲の省略可能な秒。
. 秒と秒の分数を区切る、文化に依存するシンボル。 インバリアント形式では、ピリオド (".") 文字が使用されます。
ff 1 から 7 桁の 10 進数で表される、省略可能な秒の小数部分。

入力文字列が日の値のみでない場合は、時間と分のコンポーネントを含める必要があります。その他のコンポーネントは省略可能です。 存在する場合は、各時間コンポーネントの値が指定された範囲内に収まるようにする必要があります。 たとえば、hh の値 (時間コンポーネント) は、0 から 23 の間である必要があります。 このため、"23:00:00" を Parse メソッドに渡すと、23 時間の時間間隔が返されます。 一方、"24:00:00" を渡すと、24 日間の時間間隔が返されます。 "24" は時間コンポーネントの範囲外であるため、日コンポーネントとして解釈されます。

入力文字列の構成要素は、TimeSpan.MinValue 以上、TimeSpan.MaxValue以下の時間間隔をまとめて指定する必要があります。

Parse(String) メソッドは、現在のカルチャにおける各カルチャ固有の形式を使用して、入力文字列の解析を試みます。

適用対象

Parse(String)

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

時間間隔の文字列形式を等価の TimeSpan に変換します。

public:
 static TimeSpan Parse(System::String ^ s);
public static TimeSpan Parse(string s);
static member Parse : string -> TimeSpan
Public Shared Function Parse (s As String) As TimeSpan

パラメーター

s
String

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

返品

sに対応する時間間隔。

例外

s は nullです。

s の形式が無効です。

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

-又は-

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

例

次の例では、 Parse メソッドを使用して、文字列配列内の各要素を TimeSpan 値に変換します。 現在のシステム カルチャを、現在のシステム カルチャが解析操作に与える影響を示すために、クロアチア語 ("hr-HR") と英語 - 米国 ("en-US") に変更されます。

using System;
using System.Globalization;
using System.Threading;

public class Example1
{
   public static void Main()
   {
      string[] values = { "6", "6:12", "6:12:14", "6:12:14:45",
                          "6.12:14:45", "6:12:14:45.3448",
                          "6:12:14:45,3448", "6:34:14:45" };
      string[] cultureNames = { "hr-HR", "en-US"};

      // Change the current culture.
      foreach (string cultureName in cultureNames)
      {
         Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
         Console.WriteLine("Current Culture: {0}",
                           Thread.CurrentThread.CurrentCulture.Name);
         foreach (string value in values)
         {
            try {
               TimeSpan ts = TimeSpan.Parse(value);
               Console.WriteLine("{0} --> {1}", value, ts.ToString("c"));
            }
            catch (FormatException) {
               Console.WriteLine("{0}: Bad Format", value);
            }
            catch (OverflowException) {
               Console.WriteLine("{0}: Overflow", value);
            }
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//    Current Culture: hr-HR
//    6 --> 6.00:00:00
//    6:12 --> 06:12:00
//    6:12:14 --> 06:12:14
//    6:12:14:45 --> 6.12:14:45
//    6.12:14:45 --> 6.12:14:45
//    6:12:14:45.3448: Bad Format
//    6:12:14:45,3448 --> 6.12:14:45.3448000
//    6:34:14:45: Overflow
//
//    Current Culture: en-US
//    6 --> 6.00:00:00
//    6:12 --> 06:12:00
//    6:12:14 --> 06:12:14
//    6:12:14:45 --> 6.12:14:45
//    6.12:14:45 --> 6.12:14:45
//    6:12:14:45.3448 --> 6.12:14:45.3448000
//    6:12:14:45,3448: Bad Format
//    6:34:14:45: Overflow
open System
open System.Globalization
open System.Threading

let values = 
    [| "6"; "6:12"; "6:12:14"; "6:12:14:45" 
       "6.12:14:45"; "6:12:14:45.3448" 
       "6:12:14:45,3448"; "6:34:14:45" |]
let cultureNames = [| "hr-HR"; "en-US" |]

// Change the current culture.
for cultureName in cultureNames do
    Thread.CurrentThread.CurrentCulture <- CultureInfo cultureName
    printfn $"Current Culture: {Thread.CurrentThread.CurrentCulture.Name}" 
    for value in values do
        try 
            let ts = TimeSpan.Parse value
            printfn $"{value} --> {ts:c}"
        with 
        | :? FormatException ->
            printfn $"{value}: Bad Format"
        | :? OverflowException ->
            printfn $"{value}: Overflow"
    printfn ""                                
// The example displays the following output:
//    Current Culture: hr-HR
//    6 --> 6.00:00:00
//    6:12 --> 06:12:00
//    6:12:14 --> 06:12:14
//    6:12:14:45 --> 6.12:14:45
//    6.12:14:45 --> 6.12:14:45
//    6:12:14:45.3448: Bad Format
//    6:12:14:45,3448 --> 6.12:14:45.3448000
//    6:34:14:45: Overflow
//    
//    Current Culture: en-US
//    6 --> 6.00:00:00
//    6:12 --> 06:12:00
//    6:12:14 --> 06:12:14
//    6:12:14:45 --> 6.12:14:45
//    6.12:14:45 --> 6.12:14:45
//    6:12:14:45.3448 --> 6.12:14:45.3448000
//    6:12:14:45,3448: Bad Format
//    6:34:14:45: Overflow
Dim values() As String = {"6", "6:12", "6:12:14", "6:12:14:45",
                         "6.12:14:45", "6:12:14:45.3448",
                         "6:12:14:45,3448", "6:34:14:45"}
Dim cultureNames() As String = {"hr-HR", "en-US"}

' Change the current culture.
For Each cultureName As String In cultureNames
    Thread.CurrentThread.CurrentCulture = New CultureInfo(cultureName)
    Console.WriteLine("Current Culture: {0}",
                   Thread.CurrentThread.CurrentCulture.Name)
    For Each value As String In values
        Try
            Dim ts As TimeSpan = TimeSpan.Parse(value)
            Console.WriteLine("{0} --> {1}", value, ts.ToString("c"))
        Catch e As FormatException
            Console.WriteLine("{0}: Bad Format", value)
        Catch e As OverflowException
            Console.WriteLine("{0}: Overflow", value)
        End Try
    Next
    Console.WriteLine()
Next

' The example displays the following output:
'       Current Culture: hr-HR
'       6 --> 6.00:00:00
'       6:12 --> 06:12:00
'       6:12:14 --> 06:12:14
'       6:12:14:45 --> 6.12:14:45
'       6.12:14:45 --> 6.12:14:45
'       6:12:14:45.3448: Bad Format
'       6:12:14:45,3448 --> 6.12:14:45.3448000
'       6:34:14:45: Overflow
'
'       Current Culture: en-US
'       6 --> 6.00:00:00
'       6:12 --> 06:12:00
'       6:12:14 --> 06:12:14
'       6:12:14:45 --> 6.12:14:45
'       6.12:14:45 --> 6.12:14:45
'       6:12:14:45.3448 --> 6.12:14:45.3448000
'       6:12:14:45,3448: Bad Format
'       6:34:14:45: Overflow

注釈

Parse メソッドへの入力文字列には、次の形式の時間間隔指定が含まれています。

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

角かっこ ([ および ]) の要素は省略可能です。 中かっこ ({ と }) で囲み、垂直バー (|) で区切られた代替候補の一覧から 1 つ選択する必要があります。 次の表は、それぞれの要素の説明です。

要素 説明
ws 省略可能な空白。
- 負の TimeSpanを示す省略可能な負符号。
d 0 から10675199までの日数。
。 日と時間を区切る文化に配慮したシンボル。 インバリアント形式では、ピリオド (".") 文字が使用されます。
はあ 0 から 23 までの時間。
: カルチャに依存する時刻区切り記号。 インバリアント形式では、コロン (":") 文字が使用されます。
ミリメートル 分 (0 から 59 まで)。
ss 0 ~ 59 の範囲の省略可能な秒。
. 秒と秒の分数を区切る、文化に依存するシンボル。 インバリアント形式では、ピリオド (".") 文字が使用されます。
ff 1 から 7 桁の 10 進数で表される、省略可能な秒の小数部分。

入力文字列が日の値のみでない場合は、時間と分のコンポーネントを含める必要があります。その他のコンポーネントは省略可能です。 存在する場合は、各時間コンポーネントの値が指定された範囲内に収まるようにする必要があります。 たとえば、hh の値 (時間コンポーネント) は、0 から 23 の間である必要があります。 このため、"23:00:00" を Parse メソッドに渡すと、23 時間の時間間隔が返されます。 一方、"24:00:00" を渡すと、24 日間の時間間隔が返されます。 "24" は時間コンポーネントの範囲外であるため、日コンポーネントとして解釈されます。

入力文字列の構成要素は、TimeSpan.MinValue 以上、TimeSpan.MaxValue以下の時間間隔をまとめて指定する必要があります。

Parse(String) メソッドは、現在のカルチャにおける各カルチャ固有の形式を使用して、入力文字列の解析を試みます。

適用対象