1

I have timespan : "P3M10DT5H2M" I m trying to parse with these codes:

var timespan =  "P3M10DT5H2M"
string[] formats = new[] { "'PT's'S'", "'PT'm'M'", "'P'd'D'", "'PT'h'H'", "'P'd'DT'h'H'", "'P'd'DT'h'H'm'M'", "'PT'h'H'm'M'", "'P'd'DT'h'H'm'M's'S'", "'P'm'M'd'DT'h'H'm'M'" };
TimeSpan.ParseExact((string)timespan , formats, CultureInfo.InvariantCulture);

But this error occurs: A first chance exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: String was not recognized as a valid TimeSpan.

The last format should be match for this string but not why? And also the other question my way is not so good. Because I have to write all possibilities in formats. So Should I use regular expressions instead of formats array. Please help me.

Kle
  • 1,003
  • 2
  • 14
  • 24

0 Answers0