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 181 times
0
Soner Gönül
- 94,086
- 102
- 195
- 339
Night Walker
- 19,742
- 49
- 147
- 222
4 Answers
1
Try:
DateTime t = DateTime.ParseExact("18:08:23.580", "HH:mm:ss.fff", ultureInfo.InvariantCulture);
var span = t.TimeOfDay;
Dave Bish
- 18,741
- 7
- 42
- 63
0
Parse(String, IFormatProvider)
Converts the string representation of a time interval to its TimeSpan equivalent.
More information: Here
Norbert Pisz
- 3,352
- 3
- 26
- 41
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