1

I'm looking for the following format: 00:00:00

Using the section as a reference here. This should technically work .ToString("hh:mm:ss") but it is complaining there is a syntax error.

Note: ToString("g") is close but gives me this format 0:0:0.0000.... I want the formatter to either truncate or round if the value in question is not a whole number.

The Muffin Man
  • 19,585
  • 30
  • 119
  • 191

3 Answers3

2

Try:

ToString("hh':'mm':'ss") 

Should also work:

ToString("c") 
Jmyster
  • 965
  • 2
  • 9
  • 27
  • it's for the literal charecter ":". ToString recognizes the hh mm ss. You have to tell it what you want inbetween. I literally want to put a : here. – Jmyster Jul 30 '12 at 23:36
  • When you apply the same formatter to a DateTime object why don't you have to use the literal character? – The Muffin Man Jul 30 '12 at 23:38
  • @Nick: It is explained in more detail on the MSDN page I have linked in a comment to the question. – Martin Liversage Jul 30 '12 at 23:40
  • 1
    @Nick: The ' is just escaping the :, since that is a keyword in the format string. You can also use `string.Format("{0:hh}:{0:mm}:{0:ss}, ts);` for instance, in which you see why the : needs to be escaped. – Patrick Jul 30 '12 at 23:41
2

Your syntax is almost correct, this works: ToString("hh\\:mm\\:ss");

Raxr
  • 965
  • 7
  • 12
  • 2
    Or use a verbatim string literal: `@"hh\:mm\:ss"` (I'd recommend it for the same reason it's widely used for regex strings) – Tim S. Jul 31 '12 at 00:40
0

Assuming you have TimeSpan ts;

Then try:

String.Format("{0} {1} {2}", ts.Hours, ts.Minutes, ts.Seconds);
rtuner
  • 2,362
  • 3
  • 25
  • 37