Search code examples
c#exceptiontimespanformatexception

Formatting TimeSpans


I am trying to format a TimeSpan with the following line of code:

.ToString("[d.]hh:mm:ss")

It throws a FormatException, but the exception goes away when I remove the :, the [], and the .. I also cannot include spaces. Does anyone know why this is happening? On this msdn page it clearly states that you can include these characters. I am using .Net framework 4.5.2 btw.

Thanks.


Solution

  • TimeSpan ts = new TimeSpan(5, 10, 44);
    string test = string.Format("{0:dd\\:hh\\:mm\\:ss\\.ffff}", ts);