Andrew Lackenby Andrew Lackenby - 1 year ago 123
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download