Reputation: 834
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
Here is my code:
TimeSpan timeSpan = new TimeSpan(0, h, m, s, ms);
string time = timeSpan.ToString(@"HH\:mm\:ss.fff"); // Throw a System.FormatException
It don't throw exception if I use ´@"hh:mm:ss"´ but I need the milliseconds...
What is the right string format?
I use this TimeSpan constructor.
Upvotes: 1
Views: 1282
Reputation: 118937
There's 2 problems:
HH
format specifier for TimeSpan
, use lower case version hh
(see docs).
literalWhich makes the correct version:
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");
Upvotes: 7