TimeSpan.Parse メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
時間間隔の文字列形式を等価の TimeSpan に変換します。
オーバーロード
Parse(String) |
時間間隔の文字列形式を等価の TimeSpan に変換します。 |
Parse(ReadOnlySpan<Char>, IFormatProvider) |
指定したカルチャ固有の書式情報を使用して、時間間隔のスパン表現を等価の TimeSpan に変換します。 |
Parse(String, IFormatProvider) |
指定したカルチャ固有の書式情報を使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。 |
Parse(String)
時間間隔の文字列形式を等価の 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
の形式は無効です。
例
次の例では、このメソッドを Parse 使用して、文字列配列内の各要素を値に TimeSpan 変換します。 現在のシステム カルチャをクロアチア語 ("hr-HR") と英語 - 米国 ("en-US") に変更して、現在のシステム カルチャが解析操作にどのように影響するかを示します。
using System;
using System.Globalization;
using System.Threading;
public class Example
{
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
Imports System.Globalization
Imports System.Threading
Module Example
Public Sub Main()
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
End Sub
End Module
' 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
注釈
この s
パラメーターには、次の形式の時間間隔の指定が含まれています。
[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]
角かっこ ([ および ]) 内の要素は省略可能です。 中かっこ ({ と }) で囲み、垂直バー (|) で区切られた代替候補の一覧から 1 つ選択する必要があります。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 |
- | 負 TimeSpanの符号を示す省略可能な負符号。 |
d | 0 から10675199までの日数。 |
. | 日と時間を区切るカルチャに依存する記号。 インバリアント形式では、ピリオド (".") 文字が使用されます。 |
hh | 0 から 23 までの時間。 |
: | カルチャに依存する時刻区切り記号。 インバリアント形式では、コロン (":") 文字が使用されます。 |
mm | 0 から 59 の範囲の分。 |
ss | 0 ~ 59 の範囲のオプションの秒数。 |
. | 秒を秒の小数部から区切るカルチャに依存する記号。 インバリアント形式では、ピリオド (".") 文字が使用されます。 |
Ff | 1 から 7 桁の 10 進数字で構成される秒の小数部 (省略可能)。 |
引数が s
日の値のみでない場合は、時間と分のコンポーネントを含める必要があります。他のコンポーネントは省略可能です。 存在する場合は、各時間コンポーネントの値が指定された範囲内に収まるようにする必要があります。 たとえば、 時間コンポーネントである hh の値は、0 から 23 の間である必要があります。 このため、メソッドに "23:00:00" を Parse 渡すと、23 時間の時間間隔が返されます。 一方、"24:00:00" を渡すと、24 日間の時間間隔が返されます。 "24" は時間コンポーネントの範囲外であるため、日数コンポーネントとして解釈されます。
のs
コンポーネントは、次以上の時間間隔を集合的に指定するTimeSpan.MinValueTimeSpan.MaxValue必要があります。
このメソッドは Parse(String) 、現在のカルチャのカルチャ固有の各形式を使用して解析 s
を試みます。
呼び出し元へのメモ
解析される文字列内の時間間隔コンポーネントに 7 桁を超える数字が含まれている場合、.NET Framework 3.5 以前のバージョンでの解析操作は、.NET Framework 4 以降のバージョンでの解析操作とは異なる動作をします。 場合によっては、.NET Framework 3.5 以前のバージョンで成功した解析操作が失敗し、.NET Framework 4 以降にスローOverflowExceptionされることがあります。 それ以外の場合は、.NET Framework 3.5 以前のバージョンで a をスローFormatExceptionする解析操作が失敗し、.NET Framework 4 以降でスローOverflowExceptionされる場合があります。 次の例では、両方のシナリオを示します。
string[] values = { "000000006", "12.12:12:12.12345678" };
foreach (string value in values)
{
try {
TimeSpan interval = TimeSpan.Parse(value);
Console.WriteLine("{0} --> {1}", value, interval);
}
catch (FormatException) {
Console.WriteLine("{0}: Bad Format", value);
}
catch (OverflowException) {
Console.WriteLine("{0}: Overflow", value);
}
}
// Output from .NET Framework 3.5 and earlier versions:
// 000000006 --> 6.00:00:00
// 12.12:12:12.12345678: Bad Format
// Output from .NET Framework 4 and later versions or .NET Core:
// 000000006: Overflow
// 12.12:12:12.12345678: Overflow
open System
let values = [| "000000006"; "12.12:12:12.12345678" |]
for value in values do
try
let interval = TimeSpan.Parse value
printfn $"{value} --> {interval}"
with
| :? FormatException ->
printfn $"{value}: Bad Format"
| :? OverflowException ->
printfn $"{value}: Overflow"
// Output from .NET Framework 3.5 and earlier versions:
// 000000006 --> 6.00:00:00
// 12.12:12:12.12345678: Bad Format
// Output from .NET Framework 4 and later versions or .NET Core:
// 000000006: Overflow
// 12.12:12:12.12345678: Overflow
Dim values() As String = { "000000006", "12.12:12:12.12345678" }
For Each value As String In values
Try
Dim interval As TimeSpan = TimeSpan.Parse(value)
Console.WriteLine("{0} --> {1}", value, interval)
Catch e As FormatException
Console.WriteLine("{0}: Bad Format", value)
Catch e As OverflowException
Console.WriteLine("{0}: Overflow", value)
End Try
Next
' Output from .NET Framework 3.5 and earlier versions:
' 000000006 --> 6.00:00:00
' 12.12:12:12.12345678: Bad Format
' Output from .NET Framework 4:
' 000000006: Overflow
' 12.12:12:12.12345678: Overflow
適用対象
Parse(ReadOnlySpan<Char>, IFormatProvider)
指定したカルチャ固有の書式情報を使用して、時間間隔のスパン表現を等価の 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
カルチャ固有の書式情報を提供するオブジェクト。
戻り値
formatProvider
の指定に従って変換された、input
に対応する時間間隔。
実装
適用対象
Parse(String, IFormatProvider)
指定したカルチャ固有の書式情報を使用して、時間間隔の文字列形式を等価の TimeSpan に変換します。
public:
static TimeSpan Parse(System::String ^ input, IFormatProvider ^ formatProvider);
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
カルチャ固有の書式情報を提供するオブジェクト。
戻り値
formatProvider
の指定に従って変換された、input
に対応する時間間隔。
実装
例外
input
が null
です。
input
の形式は無効です。
input
は MinValue 未満の数値か、MaxValue より大きい数値を表します。
- または -
input
の日、時間、分、または秒のコンポーネントのうち少なくとも 1 つが、その有効範囲外です。
例
次の例では、オブジェクトの CultureInfo 配列を定義し、メソッドの呼び出しで各オブジェクトを Parse(String, IFormatProvider) 使用して、文字列配列内の要素を解析します。 この例では、特定のカルチャの規則が書式設定操作にどのように影響するかを示します。
using System;
using System.Globalization;
using System.Text.RegularExpressions;
public class Example
{
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
Imports System.Globalization
Imports System.Threading
Module Example
Public Sub Main()
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
End Sub
End Module
' 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
パラメーターには、次の形式の時間間隔の指定が含まれています。
[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]
角かっこ ([ と ]) の要素は省略可能です。中かっこ ({ と }) で囲み、垂直バー (|) で区切られた代替の一覧から 1 つの選択が必要です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 |
- | 負 TimeSpanの符号を示す省略可能な負符号。 |
d | 0 から10675199までの日数。 |
. | 日と時間を区切るカルチャに依存する記号。 既定値はピリオド (".") 文字です。 |
hh | 0 から 23 までの時間。 |
: | カルチャに依存する時刻区切り記号。 |
mm | 0 から 59 の範囲の分。 |
ss | 0 ~ 59 の範囲のオプションの秒数。 |
. | 秒を秒の小数部から区切るカルチャに依存する記号。 既定値はピリオド (".") 文字です。 |
Ff | 1 から 7 桁の 10 進数字で構成される秒の小数部 (省略可能)。 |
引数が input
日の値のみでない場合は、時間と分のコンポーネントを含める必要があります。他のコンポーネントは省略可能です。 存在する場合は、各時間コンポーネントの値が指定された範囲内に収まるようにする必要があります。 たとえば、 時間コンポーネントである hh の値は、0 から 23 の間である必要があります。 このため、メソッドに "23:00:00" を Parse 渡すと、23 時間の時間間隔が返されます。 一方、"24:00:00" を渡すと、24 日間の時間間隔が返されます。 "24" は時間コンポーネントの範囲外であるため、日数コンポーネントとして解釈されます。
のinput
コンポーネントは、次以上の時間間隔を集合的に指定するTimeSpan.MinValueTimeSpan.MaxValue必要があります。
このメソッドはParse(String)、指定されたカルチャのカルチャ固有の各形式をformatProvider
使用して解析input
を試みます。
この formatProvider
パラメーターは、 IFormatProvider 返される文字列の形式に関するカルチャ固有の情報を提供する実装です。 formatProvider
パラメーターには、次のいずれかを指定できます。
CultureInfo返される文字列に書式設定規則が反映されるカルチャを表すオブジェクト。 プロパティによってCultureInfo.DateTimeFormat返されるオブジェクトはDateTimeFormatInfo、返される文字列の書式を定義します。
DateTimeFormatInfo返される文字列の書式を定義するオブジェクト。
インターフェイスを実装する IFormatProvider カスタム オブジェクト。 そのメソッドは IFormatProvider.GetFormat 、書式設定情報を DateTimeFormatInfo 提供するオブジェクトを返します。
ある場合 formatProvider
は null
、現在の DateTimeFormatInfo カルチャに関連付けられているオブジェクトが使用されます。
呼び出し元へのメモ
解析される文字列内の時間間隔コンポーネントに 7 桁を超える数字が含まれている場合、.NET Framework 3.5 以前のバージョンでの解析操作は、.NET Framework 4 以降のバージョンでの解析操作とは異なる動作をします。 場合によっては、.NET Framework 3.5 以前のバージョンで成功した解析操作が失敗し、.NET Framework 4 以降にスローOverflowExceptionされることがあります。 それ以外の場合は、.NET Framework 3.5 以前のバージョンで a をスローFormatExceptionする解析操作が失敗し、.NET Framework 4 以降でスローOverflowExceptionされる場合があります。 次の例では、両方のシナリオを示します。
string[] values = { "000000006", "12.12:12:12.12345678" };
foreach (string value in values)
{
try {
TimeSpan interval = TimeSpan.Parse(value);
Console.WriteLine("{0} --> {1}", value, interval);
}
catch (FormatException) {
Console.WriteLine("{0}: Bad Format", value);
}
catch (OverflowException) {
Console.WriteLine("{0}: Overflow", value);
}
}
// Output from .NET Framework 3.5 and earlier versions:
// 000000006 --> 6.00:00:00
// 12.12:12:12.12345678: Bad Format
// Output from .NET Framework 4 and later versions or .NET Core:
// 000000006: Overflow
// 12.12:12:12.12345678: Overflow
open System
let values = [| "000000006"; "12.12:12:12.12345678" |]
for value in values do
try
let interval = TimeSpan.Parse value
printfn $"{value} --> {interval}"
with
| :? FormatException ->
printfn $"{value}: Bad Format"
| :? OverflowException ->
printfn $"{value}: Overflow"
// Output from .NET Framework 3.5 and earlier versions:
// 000000006 --> 6.00:00:00
// 12.12:12:12.12345678: Bad Format
// Output from .NET Framework 4 and later versions or .NET Core:
// 000000006: Overflow
// 12.12:12:12.12345678: Overflow
Dim values() As String = { "000000006", "12.12:12:12.12345678" }
For Each value As String In values
Try
Dim interval As TimeSpan = TimeSpan.Parse(value)
Console.WriteLine("{0} --> {1}", value, interval)
Catch e As FormatException
Console.WriteLine("{0}: Bad Format", value)
Catch e As OverflowException
Console.WriteLine("{0}: Overflow", value)
End Try
Next
' Output from .NET Framework 3.5 and earlier versions:
' 000000006 --> 6.00:00:00
' 12.12:12:12.12345678: Bad Format
' Output from .NET Framework 4:
' 000000006: Overflow
' 12.12:12:12.12345678: Overflow