TimeSpan.TryParse Method (String, TimeSpan)


Converts the string representation of a time interval to its TimeSpan equivalent and returns a value that indicates whether the conversion succeeded.

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

static member TryParse : 
        s:string *
        result:TimeSpan byref -> bool


Type: System.String

A string that specifies the time interval to convert.

Type: System.TimeSpan

When this method returns, contains an object that represents the time interval specified by s, or TimeSpan.Zero if the conversion failed. This parameter is passed uninitialized.

Return Value

Type: System.Boolean

true if s was converted successfully; otherwise, false. This operation returns false if the s parameter is null 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 method is like the TimeSpan.Parse(String) method, except that it does not 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.




Optional white space.


An optional minus sign, which indicates a negative TimeSpan.


Days, ranging from 0 to 10675199.


A culture-sensitive symbol that separates days from hours. The invariant format uses a period (".") character.


Hours, ranging from 0 to 23.


The culture-sensitive time separator symbol. The invariant format uses a colon (":") character.


Minutes, ranging from 0 to 59.


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.


Optional fractional seconds, consisting of one to seven decimal digits.

The components of s 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 s by using each of the culture-specific formats for the current culture.

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 succeed in the .NET Framework 3.5 and earlier versions may fail and throw an OverflowException in the .NET Framework 4, The following example illustrates this scenario.

No code example is currently available or this language may not be supported.

The following example uses the TryParse method to create TimeSpan objects from valid TimeSpan strings and to indicate when the parse operation has failed because the time span string is invalid.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top