Share via


Convert TimeSpan to/from string

I never remember what to type in config file

 TimeSpanConverter tsc = new TimeSpanConverter();
TimeSpan ts = new TimeSpan( 1, 2, 3, 4, 5 ); // 1 day 2 hours 3 minutes 4 seconds 5 millisec
string s = tsc.ConvertToInvariantString( ts ); // 1.02:03:04.0050000
ts = (TimeSpan) tsc.ConvertFromInvariantString( s );
Console.WriteLine( s );

So this is the code.

Comments

  • Anonymous
    May 03, 2005
    Thanks Giulio, just what I was looking for.
  • Anonymous
    October 09, 2005
    Nice
  • Anonymous
    November 01, 2005
    Very useful. Cheers
  • Anonymous
    May 22, 2011
    Have c# application where user enters time as string in the form of 0800 hrs. how do i convert that to time; either 0800hrs or 08:00am