Formatting TimeSpans

本小妞迷上赌 提交于 2019-12-08 15:01:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!