TimeSpan ToString format

Try:

myTimeSpan.ToString("h'h 'm'm 's's'")

(Note that even spaces need to be quoted – that’s what was wrong with my first attempt.)

I’m assuming you’re using .NET 4, of course – before that, TimeSpan didn’t support custom format strings.

EDIT: As noted, this won’t work beyond 24 hours. Also note that alternatives are available via Noda Time too 🙂

Leave a Comment