About Me


Develop With Passion® - Developer Bootcamp


DateTime Formatting with single custom format specifiers

Written July 10, 2007 at 18:04 MDT Tagged .net 2.0, .net 3.0 and c sharp

I may be the last one in the .Net world to find this one out!! Say you wanted to execute the following string format statement:

return string.Format("{0:MMMM} {0:d}-{1:d}, {0:yyy}", start, end); 

The result of that format command would be: January 1/1/2007-1/7/2007, 2007. This is obviously not what I was expecting. In order to specify a single character format specifier I have to use the following:

return string.Format("{0:MMMM} {0:%d}-{1:%d}, {0:yyy}", start, end); 

Which results in the following output: January 1-7, 2007 (which is what I wanted).

Notice the use of the % format specifier prior to the single digit day format specifier. That is what I was missing. If you want to know more about how this works check out the documentation here.aspx#UsingSingleSpecifiers).

Develop with passion!