Export (0) Print
Expand All

TimeSpan.TryParse Method

Updated: July 2008

Constructs a new TimeSpan object from a time interval specified in a string. Parameters specify the time interval and the variable where the new TimeSpan object is returned.

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

public static function TryParse(
	s : String, 
	result : TimeSpan
) : boolean


Type: System.String

A string that specifies a time interval.

Type: System.TimeSpan%

When this method returns, contains an object that represents the time interval specified by s, or 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 a null reference (Nothing in Visual Basic), has an invalid format, represents a time interval less than MinValue or greater than 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 the TryParse method does not throw an exception if the conversion fails.

The s parameter contains a time interval specification of the form:

[ws][-]{ d | d.hh:mm[:ss[.ff]] | hh:mm[:ss[.ff]] }[ws]

Items in square brackets ([ and ]) are optional. One selection from the list of alternatives enclosed in braces ({ and }) and separated by vertical bars (|) is required. Colons and periods (: and .) are literal characters and required. Other items are as follows.




Optional white space.


Optional minus sign indicating a negative TimeSpan.


Days, ranging from 0 to 10675199.


Hours, ranging from 0 to 23.


Minutes, ranging from 0 to 59.


Optional seconds, ranging from 0 to 59.


Optional fractional seconds, consisting of 1 to 7 decimal digits.

The components of s must collectively specify a time interval greater than or equal to MinValue and less than or equal to MaxValue.

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.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0




July 2008

Added an example.

Customer feedback.

Community Additions

© 2014 Microsoft