共用方式為


TimeSpan.Parse Method (String, IFormatProvider)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific format information.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function Parse ( _
    input As String, _
    formatProvider As IFormatProvider _
) As TimeSpan
public static TimeSpan Parse(
    string input,
    IFormatProvider formatProvider
)

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.TimeSpan
A time interval that corresponds to input, as specified by formatProvider.

Exceptions

Exception Condition
ArgumentNullException

input is nulla null reference (Nothing in Visual Basic).

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.

Remarks

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 first tries to parse input by using the invariant format. If this is not successful, it then tries 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:

If formatProvider is nulla null reference (Nothing in Visual Basic), the DateTimeFormatInfo object that is associated with the current culture is used.

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.