Format a TimeSpan

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
 
L

Larry Lard

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top