Andrew Lackenby Andrew Lackenby - 6 days ago 6
C# Question

Error parsing time with TimeSpan.ParseExact

I'm trying to read in 2 columns from a DataTable that will give me a date and time.

I've tried the line of code:

lastRun = DateTime.ParseExact(row["last_run_date"].ToString(), "yyyyMMdd", CultureInfo.InvariantCulture)
.Add(TimeSpan.ParseExact(row["last_run_time"].ToString().PadLeft(6, '0'), "HHmmss", CultureInfo.InvariantCulture));


The last_run_date part works OK but the time will not parse correctly.

Answer

For TimeSpan, "HH" is not a valid format specifier - use "hh" instead.

https://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx

Comments