System.TimeSpan.TryParse methods
This article provides supplementary remarks to the reference documentation for this API.
TryParse(System.String,System.TimeSpan@) method
The TimeSpan.TryParse(String, TimeSpan) method is like the TimeSpan.Parse(String) method, except that it doesn't throw an exception if the conversion fails.
The s
parameter contains a time interval specification in the form:
[ws][-]{ d | d.hh:mm[:ss[.ff]] | 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. |
The components of s
must collectively specify a time interval that's greater than or equal to TimeSpan.MinValue and less than or equal to TimeSpan.MaxValue.
The Parse(String) method tries to parse s
by using each of the culture-specific formats for the current culture.
TryParse(String, IFormatProvider, TimeSpan) method
The TryParse(String, IFormatProvider, TimeSpan) method is like the Parse(String, IFormatProvider) method, except that it does not throw an exception if the conversion fails.
The input
parameter contains a time interval specification in the form:
[ws][-]{ d | d.hh:mm[:ss[.ff]] | 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. |
The components of input
must collectively specify a time interval that is greater than or equal to TimeSpan.MinValue and less than or equal to TimeSpan.MaxValue.
The TryParse(String, IFormatProvider, TimeSpan) method tries to parse input
by using each of the culture-specific formats for the culture specified by formatProvider
.
The formatProvider
parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string. The formatProvider
parameter can be any of the following:
- A CultureInfo object that represents the culture whose formatting conventions are to be reflected in the returned string. The DateTimeFormatInfo object returned by the CultureInfo.DateTimeFormat property defines the formatting of the returned string.
- A DateTimeFormatInfo object that defines the formatting of the returned string.
- A custom object that implements the IFormatProvider interface. Its IFormatProvider.GetFormat method returns a DateTimeFormatInfo object that provides formatting information.
If formatProvider
is null
, the DateTimeFormatInfo object that is associated with the current culture is used.
Notes to callers
In some cases, when a time interval component in the string to be parsed contains more than seven digits, parsing operations that succeeded and returned true
in .NET Framework 3.5 and earlier versions may fail and return false
in .NET Framework 4 and later versions. The following example illustrates this scenario:
string value = "000000006";
TimeSpan interval;
if (TimeSpan.TryParse(value, out interval))
Console.WriteLine("{0} --> {1}", value, interval);
else
Console.WriteLine("Unable to parse '{0}'", value);
// Output from .NET Framework 3.5 and earlier versions:
// 000000006 --> 6.00:00:00
// Output from .NET Framework 4:
// Unable to parse //000000006//
let value = "000000006"
match TimeSpan.TryParse value with
| true, interval ->
printfn $"{value} --> {interval}"
| _ ->
printfn $"Unable to parse '{value}'"
// Output from .NET Framework 3.5 and earlier versions:
// 000000006 --> 6.00:00:00
// Output from .NET Framework 4:
// Unable to parse //000000006//
Dim value As String = "000000006"
Dim interval As TimeSpan
If TimeSpan.TryParse(value, interval) Then
Console.WriteLine("{0} --> {1}", value, interval)
Else
Console.WriteLine("Unable to parse '{0}'", value)
End If
' Output from .NET Framework 3.5 and earlier versions:
' 000000006 --> 6.00:00:00
' Output from .NET Framework 4:
' Unable to parse '000000006'