问题
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.
回答1:
TimeSpan ts = new TimeSpan(5, 10, 44);
string test = string.Format("{0:dd\\:hh\\:mm\\:ss\\.ffff}", ts);
回答2:
You need to escape the literal characters. It's quite awkward but this is what you need.
TimeSpan ts = new TimeSpan(1, 2, 3, 4, 555);
string output = ts.ToString("d\\.hh\\:mm\\:ss");
See Docs here.
来源:https://stackoverflow.com/questions/24328466/formatting-timespans