Format a TimeSpan

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'd like to convert a TimeSpan value to a String - but without seconds (e.g.
"12:01" for 12 hours and one minute). Is there a way to use String.Format()
or other functions to do this?

Thanks,
Guido
 
Guido said:
Hi,

I'd like to convert a TimeSpan value to a String - but without seconds (e.g.
"12:01" for 12 hours and one minute). Is there a way to use String.Format()
or other functions to do this?

You could convert to a DateTime and use that class's extensive
formatting available in ToString:

TimeSpan ts = new TimeSpan(12, 1, 14);
Console.WriteLine (ts.ToString());
DateTime dt = new DateTime(ts.Ticks);
Console.WriteLine (dt.ToString("hh:mm"));

output:
12:01:14
12:01

but maybe there is a cleaner way.
 
Back
Top