TimeSpan.TryParse Method

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 bool TryParse (
	string s,
	out TimeSpan result
public static boolean TryParse (
	String s, 
	/** @attribute OutAttribute() */ /** @ref */ TimeSpan result
Not applicable.



A string that specifies a time interval.


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

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 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.

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions