System.FormatException在TimeSpan.ToString() [英] System.FormatException on TimeSpan.ToString()
本文介绍了System.FormatException在TimeSpan.ToString()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个代表秒数的浮点数,我需要对其进行格式化以使其与此匹配:
I have a float that represent a quantity of seconds and I need to format it to match this:
我需要这样设置经过时间(以秒为单位):
I need to format an elapsed time (in seconds) like this:
HH:mm:ss.fff // Like 01:15:22.150
这是我的代码:
TimeSpan timeSpan = new TimeSpan(0, h, m, s, ms);
string time = timeSpan.ToString(@"HH\:mm\:ss.fff"); // Throw a System.FormatException
如果我使用´@"hh:mm:ss"´不会抛出异常,但是我需要毫秒...
It don't throw exception if I use ´@"hh:mm:ss"´ but I need the milliseconds...
什么是正确的字符串格式?
What is the right string format?
我使用此 TimeSpan 构造函数.
推荐答案
有2个问题:
- There is no
HH
format specifier forTimeSpan
, use lower case versionhh
(see docs) - You need to escape the
.
literal
哪个版本正确?
string time = timeSpan.ToString(@"hh\:mm\:ss\.fff");
您还可以通过用'
括起文字字符串来指定文字字符串.例如:
You can also specify literal strings by surrounding them with '
. For example:
string time = timeSpan.ToString("hh':'mm':'ss'.'fff");
这篇关于System.FormatException在TimeSpan.ToString()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文