formatting numbers with leading zeros

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

How can I format numbers with leading zeros? I believe I did this before
some time ago but I cannot remember how I did it.
I want the output be 100305 for 10:03:05.

string fname = string.Format("{0}{1}{2}.txt", dt.Hour, dt.Minute,
dt.Second);

I do not want to use ToString() because it generated to much temporary
objects, and DateTime.ToShortTimeString() is culture dependend and contains
colon characters.
 
use string.Format("{0:hh}{0:mm}{0:ss}.txt", dt);
or

use string.Format("{0:HH}{0:mm}{0:ss}.txt", dt);

/LM
 
string fname = string.Format("{0:HHmmss}.txt", dt);
NOTE: HH above formats hour as 00-23, hh formats hour as 01-12.

Alternatively you can use:


thanks thats exactly what I was looking for!
 
Back
Top