Format .NET DateTime “Day” with no leading zero

后端 未结 2 1888
陌清茗
陌清茗 2020-12-01 23:44

For the following code, I would expect result to equal 2, because the MSDN states that \'d\' \"Represents the day of the month as a number from 1 through 31. A sing

2条回答
  •  萌比男神i
    2020-12-02 00:12

    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

提交回复
热议问题