Why does TimeSpan.ParseExact not work

Viewed 28331

This is a bit wierd. Parsing a text field with a valid timespan fails if I try to be precise!

const string tmp = "17:23:24";
//works
var t1 = TimeSpan.Parse(tmp);
//fails
var t2 = TimeSpan.ParseExact(tmp, "hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

The second parse fails with an exception "Input string was not in a correct format." from DateTime.

6 Answers

Use this It works use

TimeSpan.ParseExact(value, “h\\:mm”, CultureInfo.InvariantCulture);
Related