TimeSpan.Parse Method (String, IFormatProvider)
Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific format information.
Assembly: mscorlib (in mscorlib.dll)
Public Shared Function Parse ( input As String, formatProvider As IFormatProvider ) As TimeSpan
Parameters
- input
-
Type:
System.String
A string that specifies the time interval to convert.
- formatProvider
-
Type:
System.IFormatProvider
An object that supplies culture-specific formatting information.
Return Value
Type: System.TimeSpanA time interval that corresponds to input, as specified by formatProvider.
| Exception | Condition |
|---|---|
| ArgumentNullException | input is null. |
| FormatException | input has an invalid format. |
| OverflowException | input represents a number that is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. -or- At least one of the days, hours, minutes, or seconds components in input is outside its valid range. |
The input parameter 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 default value is a period (".") character. |
hh | Hours, ranging from 0 to 23. |
: | The culture-sensitive time separator symbol. |
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 default value is 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 Parse(String) 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:
When a time interval component in the string to be parsed contains more than seven digits, parsing operations in the .NET Framework 3.5 and earlier versions may behave differently from parsing operations in the .NET Framework 4. In some cases, parsing operations that succeed in the .NET Framework 3.5 and earlier versions may fail and throw an OverflowException in the .NET Framework 4. In other cases, parsing operations that throw a FormatException in the .NET Framework 3.5 and earlier versions may fail and throw an OverflowException in the .NET Framework 4. The following example illustrates both scenarios.
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
The following example defines an array of CultureInfo objects, and uses each object in calls to the Parse(String, IFormatProvider) method to parse the elements in a string array. The example illustrates how the conventions of a specific culture influence the formatting operation.
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
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 7.1
Windows Phone
Available since 8.1