Search code examples
c#datedatetimeformattingleading-zero

How do I format a date without a leading zero in month?


The code is

time.ToString("yyyyMMddHmmsFFF"));

And it returns "20190121153530" with leading zero after 2019.

How do I format the date to get "2019121153530" without the leading zero?


Solution

  • MM in the format you specified will use "The month, from 01 through 12".

    M in the format will use "The month, from 1 through 12". Such as:

    time.ToString("yyyyMddHmmsFFF"));
    

    See "Custom Date and Time Format Strings" for more options on DateTime string formatting.