I have this time string 18:08:23.580
the pattern seems to be HH:mm:ss.fff
How I can convert this string using my pattern to TimeSpan
?
Asked
Active
Viewed 195 times
0

Soner Gönül
- 97,193
- 102
- 206
- 364

Night Walker
- 20,638
- 52
- 151
- 228
4 Answers
1
Try:
DateTime t = DateTime.ParseExact("18:08:23.580", "HH:mm:ss.fff", ultureInfo.InvariantCulture);
var span = t.TimeOfDay;

Dave Bish
- 19,263
- 7
- 46
- 63
0
Parse(String, IFormatProvider)
Converts the string representation of a time interval to its TimeSpan equivalent.
More information: Here

Norbert Pisz
- 3,392
- 3
- 27
- 42
0
Looks like this is the way to go:
TimeSpan ts = TimeSpan.ParseExact(value, @"hh\:mm\:ss\.fff", CultureInfo.InvariantCulture);
See also: Why does TimeSpan.ParseExact not work