Format .NET DateTime "Day" with no leading zero

Rather than using string formatting strings, how about using the Day property

DateTime myDate = new DateTime(2009,6,4)
int result = myDate.Day;

Or if you really needed the result in string format

string result = myDate.Day.ToString();

If you are looking to get a specific date part out of a date object rather than a formatted representation of the date, I prefer to use the properties (Day, Month, Year, DayOfWeek, etc.) It makes reading the code a bit easier (particularly if someone else is reading/maintaining it that doesn't have the various formatting codes memorized)


To indicate that this is a custom format specifier (in contrast to a standard format specifier), it must be two characters long. This can be accomplished by adding a space (which will show up in the output), or by including a percent sign before the single letter, like this:

string result = myDate.ToString("%d");

See documentation