System.TimeSpan.Parse method
This article provides supplementary remarks to the reference documentation for this API.
The input string to the Parse methods contains a time interval specification in the form:
[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]
Elements in square brackets ([
and ]
) are optional. One selection from the list of alternatives enclosed in braces ({
and }
) and separated by vertical bars (|) is required. The following table describes each element.
Element | Description |
---|---|
ws | Optional white space. |
- | An optional minus sign, which indicates a negative TimeSpan. |
d | Days, ranging from 0 to 10675199. |
. | A culture-sensitive symbol that separates days from hours. The invariant format uses a period (".") character. |
hh | Hours, ranging from 0 to 23. |
: | The culture-sensitive time separator symbol. The invariant format uses a colon (":") character. |
mm | Minutes, ranging from 0 to 59. |
ss | Optional seconds, ranging from 0 to 59. |
. | A culture-sensitive symbol that separates seconds from fractions of a second. The invariant format uses a period (".") character. |
ff | Optional fractional seconds, consisting of one to seven decimal digits. |
If the input string is not a day value only, it must include an hours and a minutes component; other components are optional. If they are present, the values of each time component must fall within a specified range. For example, the value of hh, the hours component, must be between 0 and 23. Because of this, passing "23:00:00" to the Parse method returns a time interval of 23 hours. On the other hand, passing "24:00:00" returns a time interval of 24 days. Because "24" is outside the range of the hours component, it is interpreted as the days component.
The components of the input string must collectively specify a time interval that is greater than or equal to TimeSpan.MinValue and less than or equal to TimeSpan.MaxValue.
The Parse(String) method tries to parse the input string by using each of the culture-specific formats for the current culture.
Notes to callers
When a time interval component in the string to be parsed contains more than seven digits, parsing operations in .NET Framework 3.5 and earlier versions may behave differently from parsing operations in .NET Framework 4 and later versions. In some cases, parsing operations that succeed in .NET Framework 3.5 and earlier versions may fail and throw an OverflowException in .NET Framework 4 and later. In other cases, parsing operations that throw a FormatException in .NET Framework 3.5 and earlier versions may fail and throw an OverflowException in .NET Framework 4 and later. The following example illustrates both scenarios.
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