How can I String.Format a TimeSpan object with a custom format in .NET?

I'm surprised that nobody has suggested this, so I've added this answer for completeness.

I'm surprised that nobody has suggested this, so I've added this answer for completeness. . Net 4 allows you to use custom format strings with Timespan.

You can find a full reference of available format specifiers at the MSDN Custom TimeSpan Format Strings page. Here's an example timespan format string: string. Format("{0:hh\\:mm\\:ss}", myTimeSpan); //example output 15:36:15 You need to escape the ":" character with a "\" (which itself must be escaped unless you're using a verbatim string).

This excerpt from the MSDN Custom TimeSpan Format Strings page explains about escaping the ":" and ". " characters in a format string: The custom TimeSpan format specifiers do not include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in the custom format string as string literals.

For example, "dd. Hh\:mm" defines a period (.) as the separator between days and hours, and a colon (:) as the separator between hours and minutes.

2 This does NOT work on . NET 3.5 and below. – Andrei Rinea Jul 13 '11 at 9:07 @Andrei Rinea: Correct, as stated at the start of my second paragraph ".

Net 4 allows you to use custom format strings with Timespan". – DoctaJonez Jul 13 '11 at 9:49 Yeah I saw that, just trying to point out to other people. I tried it in 3.5 hoping it's not a v4 only feature.

– Andrei Rinea Jul 13 '11 at 12:38.

For . NET 3.5 and lower you could use: string. Format ("{0:00}:{1:00}:{2:00}", (int)myTimeSpan.

TotalHours, myTimeSpan. Minutes, myTimeSpan. Seconds); Code taken from a Jon Skeet answer on bytes For .

NET 4.0 and above, see DoctaJonez answer.

Yes, thank you. But I think that DateTime approach is more customizable, as it would work for any time format supported by DateTime. This approach is hard to use for showing AM/PM for example.

– Hosam Aly Feb 22 '09 at 13:21 1 Sure, TimeSpan is meant to represents a period of time, not a time of day (Even though the DateTime.Now. TimeOfDay property would have you believe otherwise). If you need to represent a specific time of day I suggest you continue using the DateTime class.

– JohannesH Feb 22 '09 at 13:41 6 Just remember that if the TimeSpan is equal to or more than 24 hours you will get incorrect formatting. – JohannesH Feb 22 '09 at 13:42 1 If you pass 2 hours and 40 minutes it will display 03:40 since Totalhours will be 2.67 – jvanderh 2.671 at 14:36 1 This is the best solution for . NET 3.5 and below.

– Andrei Rinea 2.672 at 9:08.

One way is to create a DateTime object and use it for formatting: new DateTime(myTimeSpan. Ticks). ToString(myCustomFormat) // or using String.

Format: String. Format("{0:HHmmss}", new DateTime(myTimeSpan. Ticks)) This is the way I know.

I hope someone can suggest a better way.

12 This is really only going to work if the TimeSpan is less than a day. That might not be a such a terrible restriction, but it keeps it from being a general solution. – tvanfosson Feb 22 '09 at 13:17 Good point @tvanfosson.

Thank you. – Hosam Aly Feb 22 '09 at 13:30.

Simple. Use TimeSpan. ToString with c, g or G.

More information at msdn.microsoft.com/en-us/library/ee37228....

Thank you for your answer. This method is apparently new in . NET 4, and did not exist when the question was asked.It also does not support custom formats.

Nevertheless, it's a valuable addition to the answers to this questions. Thanks again. – Hosam Aly Aug 10 '10 at 19:44.

This is awesone one: string. Format ("{0:00}:{1:00}:{2:00}", myTimeSpan. TotalHours, myTimeSpan.

Minutes, myTimeSpan. Seconds).

You need to cast the myTimeSpan. TotalHours to an int - otherwise it might get rounded-up. See JohannesH's answer – codeulike Sep 7 '10 at 13:44.

. NET Format String 102: DateTime Format String.

4 TimeSpan! = DateTime. – Richard Feb 22 '09 at 14:11.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions