0

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 ?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Night Walker
  • 20,638
  • 52
  • 151
  • 228

4 Answers4

4

You could simpy use TimeSpan.Parse without an explicit pattern:

TimeSpan.Parse("18:08:23.580")

Demo

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
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

And: http://msdn.microsoft.com/en-us/library/ee372287.aspx

Community
  • 1
  • 1
JLRishe
  • 99,490
  • 19
  • 131
  • 169