I have a DataTable dtTest.
I want to parse one cell from this table from row 2 and colum 2
This cell can have a format of hh:mm:ss or h:mm:ss
I want to parse it to switch to format h.mm or hh.mm
here I verify if there is symbol “:” or not on position 2
string typeTime = dtTest.Rows[2][2].ToString().Substring(1, 1);
Now I parse them:
TimeSpan.ParseExact(dtTest.Rows[2][2].ToString(),
typeTime == "." ? "h'.'mm" : "hh'.'mm", CultureInfo.InvariantCulture);
After parsing it gives me an error “Input string was not in a correct format“.
You are trying to parse a string exactly – your string contains a seconds component that does not exist in your format strings. Your string also contains
:where you specify..The following should work:
Note that the
hformat specifier will correctly understand8or08.Additionally, you can simply use one of the standard
TimeSpanformat strings – specificallygORcinstead of the custom format string: