This is my input:
55
This is my desired output:
PT55H
Is there a built in class in C# that converts a timespan as: TimeSpan.TryParse(55)
or as a string "55" with hours into an ISO8601 formatted string?
This is my input:
55
This is my desired output:
PT55H
Is there a built in class in C# that converts a timespan as: TimeSpan.TryParse(55)
or as a string "55" with hours into an ISO8601 formatted string?
You can convert a number to a TimeSpan
with the static TimeSpan.FromHours
method. For example var ts = TimeSpan.FromHours(55.0);
.
If you always want the time represented with hours only, in the ISO system, maybe you can simply say var isostring = String.Format("PT{0}H", ts.TotalHours);
.
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!