TimeSpan::TryParse Method (String, IFormatProvider, TimeSpan%)
Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific formatting information, and returns a value that indicates whether the conversion succeeded.
Assembly: mscorlib (in mscorlib.dll)
public: static bool TryParse( String^ input, IFormatProvider^ formatProvider, [OutAttribute] TimeSpan% result )
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.
- result
- Type: System::TimeSpan%
When this method returns, contains an object that represents the time interval specified by input, or TimeSpan::Zero if the conversion failed. This parameter is passed uninitialized.
Return Value
Type: System::Booleantrue if input was converted successfully; otherwise, false. This operation returns false if the input parameter is nullptr or String::Empty, has an invalid format, represents a time interval that is less than TimeSpan::MinValue or greater than TimeSpan::MaxValue, or has at least one days, hours, minutes, or seconds component outside its valid range.
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 nullptr, the DateTimeFormatInfo object that is associated with the current culture is used.
Notes to CallersIn some cases, when a time interval component in the string to be parsed contains more than seven digits, parsing operations that succeed and return true in the .NET Framework 3.5 and earlier versions may fail and return false in the .NET Framework 4, The following example illustrates this scenario.
The following example defines an array of CultureInfo objects, and uses each object in calls to the TryParse(String, IFormatProvider, TimeSpan%) method to parse the elements in a string array. The example illustrates how the conventions of a specific culture influence the formatting operation.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.