How do I convert an ISO8601 TimeSpan to a C# TimeSpan?
You need to add the Time separator to your string. Try this:
TimeSpan ts = XmlConvert.ToTimeSpan("PT72H");
See the duration specification - http://www.w3.org/TR/xmlschema-2/#duration
3.2.6.1 Lexical representation
The lexical representation for duration is the [ISO 8601] extended format PnYn MnDTnH nMnS, where nY represents the number of years, nM the number of months, nD the number of days, 'T' is the date/time separator, nH the number of hours, nM the number of minutes and nS the number of seconds. The number of seconds can include decimal digits to arbitrary precision.
Edit/Update based on comments
As there was some question as to why the string P2M2W5D
would not be considered a valid TimeSpan
since W
is part of the ISO 8601 standard, I wanted to add this update so that if someone runs across that issue they don't have to read through the comments to get the answer. The issue, both for the original string in question P72H
and P2M2W5D
is that the string must conform to the W3C XML Schema (see the documentation for XmlConvert.ToTimeSpan). When we look at the W3C XML Schema (link above), it references back to the ISO 8601 standard, and in particular to section 5.5.3.2.1 which gives the reason why W
is not a valid character in the XML Schema:
Since weeks have no defined carry-over point (52 or 53), weeks should
not be used in these applications
Converting a specific ISO8601 TimeSpan (P2M2W5D) to a C# TimeSpan
From XmlConvert.ToTimeSpan
method
Parameters
s
Type: System.StringThe string to convert. The string format must conform to the W3C XML Schema Part 2: Datatypes recommendation for duration.
And Duration section
The lexical representation for duration is the [ISO 8601] extended
formatPnYn MnDTnH nMnS
, wherenY
represents the number of years,nM
the number of months,nD
the number of days,'T'
is the date/time
separator,nH
the number of hours,nM
the number of minutes andnS
the
number of seconds.
From ISO 8601 Date and Time Formats
In the lexical format for duration the following characters are also
used as designators and appear as themselves in lexical formats:
- P -- is used as the time duration designator, preceding a data element representing a given duration of time.
- Y -- follows the number of years in a time duration.
- M -- follows the number of months or minutes in a time duration.
- D -- follows the number of days in a time duration.
- H -- follows the number of hours in a time duration.
- S -- follows the number of seconds in a time duration.
As far as I can see, there is no W
as a duration format in XML specification.
This works for example;
TimeSpan ts = XmlConvert.ToTimeSpan("P2M5D");
TimeSpan to ISO8601 duration format string
found the solution myself, so I thought I'd share:
TimeSpan timeSpan = new TimeSpan(0, value, 0);
return XmlConvert.ToString(timeSpan);
How to Convert ISO 8601 Duration to TimeSpan in VB.Net?
This will convert from xs:duration to TimeSpan:
System.Xml.XmlConvert.ToTimeSpan("P0DT1H0M0S")
See http://msdn.microsoft.com/en-us/library/system.xml.xmlconvert.totimespan.aspx
How do I convert an integer into an ISO8601 TimeSpan?
Coworker just found this for me:
TimeSpan start = new TimeSpan(int.Parse(txtStartHours.Text), 0, 0);
durationNode.Element("StartTime").Value = XmlConvert.ToString(start);
It seems to convert it to PT2D7H
, but since I'm using XMLConvert.ToTimeSpan().TotalHours
elsewhere, it shouldn't cause any problems!
Parse YouTube ISO 8601 to DateTime C#
TimeSpan ts = XmlConvert.ToTimeSpan("PT5M58S");
Best option is to convert to a TimeSpan instead of a DateTime.
From the MS docs on TimeSpan (https://msdn.microsoft.com/library/system.timespan):
Represents a time interval.
Reading PT0S to 0 second using C#
You can't directly convert a timespan to a decimal. You need to decide which property you want.
See How do I convert an ISO8601 TimeSpan to a C# TimeSpan? (XmlConvert.ToTimeSpan(string)
), and then for example save TimeSpan.TotalSeconds
in your decimal column.
Related Topics
Inheritance with Base Class Constructor with Parameters
How to Get a Uri of the Image Stored in the Resources
Zooming Graphics Based on Current Mouse Position
Converting a Generic List to a CSV String
Force Gui Update from UI Thread
How to Execute Process Commands (Or Similar) Using a Universal Windows Platform (Uwp) App
Yield Statement Implementation
Inconsistency in Divide-By-Zero Behavior Between Different Value Types
How to Bind Crystal Report to Manually Created Dataset
Two-Way/Bidirectional Dictionary in C#
ASP.NET Asmx Web Service Returning Xml Instead of JSON