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
  • 94,086
  • 102
  • 195
  • 339
Night Walker
  • 19,742
  • 49
  • 147
  • 222

4 Answers4

4

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

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

Demo

Tim Schmelter
  • 429,027
  • 67
  • 649
  • 891
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

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

Community
  • 1
  • 1
JLRishe
  • 95,368
  • 17
  • 122
  • 158