This topic has not yet been rated - Rate this topic

XmlConvert.ToTimeSpan Method

Converts the String to a TimeSpan equivalent.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
public static TimeSpan ToTimeSpan(
	string s
)

Parameters

s
Type: System.String
The string to convert. The string format must conform to the W3C XML Schema Part 2: Datatypes recommendation for duration.

Return Value

Type: System.TimeSpan
A TimeSpan equivalent of the string.
Exception Condition
FormatException

s is not in correct format to represent a TimeSpan value.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, 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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Cannot fully support XSD standard
Beware! This method cannot fully support the standard as specified in XML Schema Part 2: Datatypes Second Edition. For example, if you pass "P1Y" to it, it will return a TimeSpan for 365 days. However, in section 3.2.6.2 of the standard, which deals with how to order (i.e., compare) durations, there is a table which explicitly documents that "P1Y" has an 'indeterminate' comparison with both 365 and 366. This method, however, returns equal TimeSpans for "P1Y" and "P365D".  XmlConvert maps XSD data types onto CLR data types, but can't deal with some of the 'impedance mismatches' that arise.
ISO 8601 Duration
According to the SO link below this method parses ISO 8601 duration strings. http://stackoverflow.com/questions/62804/how-to-convert-iso-8601-duration-to-timespan-in-vb-net.  Like "PT2H30M10S".