<p>Is there a standard library method that converts a string that has duration in the standard ISO 8601 Duration (also used in XSD for its <code>duration</code> type) format into the .NET TimeSpan object?</p>

<p>For example, P0DT1H0M0S which represents a duration of one hour, is converted into New TimeSpan(0,1,0,0,0).</p>

<p>A Reverse converter does exist which works as follows:
Xml.XmlConvert.ToString(New TimeSpan(0,1,0,0,0))
The above expression will return P0DT1H0M0S.</p>

[{'answer_id': 63219, 'author': 'user7658', 'author_id': 7658, 'author_profile': 'https://Stackoverflow.com/users/7658', 'pm_score': 6, 'selected': True, 'text': '<p>This will convert from xs:duration to TimeSpan:</p>\n\n<pre><code>System.Xml.XmlConvert.ToTimeSpan("P0DT1H0M0S")\n</code></pre>\n\n<p>See <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlconvert.totimespan.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.xml.xmlconvert.totimespan.aspx</a></p>\n'}, {'answer_id': 5760821, 'author': 'Paul Williams', 'author_id': 420400, 'author_profile': 'https://Stackoverflow.com/users/420400', 'pm_score': 4, 'selected': False, 'text': '<p>One minor word of caution - XmlConvert.ToTimeSpan() is a little funny when working with months and years.  The TimeSpan class does not have month or year members, probably because their length varies.  However, ToTimeSpan() will happily accept a duration string with month or year values in it and <strong><em>guess</em></strong> at a duration, instead of throwing an exception.  Observe:</p>\n\n<pre><code>PS C:\\Users\\troll&gt; [Reflection.Assembly]::LoadWithPartialName("System.Xml")\n\nGAC    Version        Location\n---    -------        --------\nTrue   v2.0.50727     C:\\Windows\\assembly\\GAC_MSIL\\System.Xml\\2.0.0.0__b77a5c561934e089\\System.Xml.dll\n\n\nPS C:\\Users\\troll&gt; [System.Xml.XmlConvert]::ToTimeSpan("P1M")\n\n\nDays              : 30\nHours             : 0\nMinutes           : 0\nSeconds           : 0\nMilliseconds      : 0\nTicks             : 25920000000000\nTotalDays         : 30\nTotalHours        : 720\nTotalMinutes      : 43200\nTotalSeconds      : 2592000\nTotalMilliseconds : 2592000000\n\n\n\nPS C:\\Users\\troll&gt; [System.Xml.XmlConvert]::ToTimeSpan("P1Y")\n\n\nDays              : 365\nHours             : 0\nMinutes           : 0\nSeconds           : 0\nMilliseconds      : 0\nTicks             : 315360000000000\nTotalDays         : 365\nTotalHours        : 8760\nTotalMinutes      : 525600\nTotalSeconds      : 31536000\nTotalMilliseconds : 31536000000\n\n\n\nPS C:\\Users\\troll&gt;\n</code></pre>\n'}, {'answer_id': 11923534, 'author': 'revington', 'author_id': 344911, 'author_profile': 'https://Stackoverflow.com/users/344911', 'pm_score': 0, 'selected': False, 'text': '<p>As @ima dirty troll said TimeSpan translates always years as 365 days and months as 30 days.</p>\n\n<pre><code>TimeSpan ts = System.Xml.XmlConvert.ToTimeSpan("P5Y");\nDateTime now = new DateTime(2008,2,29);\nConsole.WriteLine(now + ts); // 27/02/2013 0:00:00\n</code></pre>\n\n<p>To address it you should add each field individually rather\nthan using TimeSpan.</p>\n\n<pre><code>DateTime now = new DateTime (2008, 2, 29);\nstring duration = "P1Y";\nRegex expr = \n    new Regex (@"(-?)P((\\d{1,4})Y)?((\\d{1,4})M)?((\\d{1,4})D)?(T((\\d{1,4})H)?((\\d{1,4})M)?((\\d{1,4}(\\.\\d{1,3})?)S)?)?", RegexOptions.Compiled | RegexOptions.CultureInvariant);\nbool positiveDuration = false == (input [0] == \'-\');\n\nMatchCollection matches = expr.Matches (duration);\nvar g = matches [0];\nFunc&lt;int,int&gt; getNumber = x =&gt; {\n    if (g.Groups.Count &lt; x || string.IsNullOrEmpty (g.Groups [x].ToString ())) {\n        return 0;\n    }\n\n    int a = int.Parse (g.Groups [x].ToString ());\n\n    return PositiveDuration ? a : a * -1;\n\n};\nnow.AddYears (getNumber (3));\nnow.AddMonths (getNumber (5));\nnow.AddDays (getNumber (7));\nnow.AddHours (getNumber (10));\nnow.AddMinutes (getNumber (12));\nnow.AddSeconds (getNumber (14));\nConsole.WriteLine (now); // 28/02/2012 0:00:00\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/62804', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7105/']