String format for getting the day of the month without a leading zero

I am trying to use string formatting in an object DateTimein C # to get the day of the month without leading zeros.

After some searching, I found some documentation for DateTime formats that shows that I would have to call:

dateTime.ToString("d");

to get the value that I need, but when I tried it, instead of getting numbers like this 28, the yield was: 3/28/2013.

After a bit more searching, I found documentation for standard date and time formats .

Both sets of formats use the format "d", but the results show that the standard string format takes precedence over the custom string format.

Can I specify which formatting is desired?

To coordinate the code, I would prefer to write:

a = dateTime.ToString(...some format...);
b = dateTime.ToString(...some format...);

but not

a = dateTime.ToString(...some format...);
b = dateTime.Day.ToString();
+5
source share
2 answers

A single character refers to a standard date and time format string , in this case a “short date pattern”.

In order for it to be interpreted as user-defined, you need to prefix the user-format string with the symbol %:

dateTime.ToString("%d");

This is described on the page you linked to, using the specifications of the individual custom formats (which describes the custom format specifier %).

+10
source

ToString , :

dateTime.ToString("''d")
0

All Articles