Python datetime: strftime() codes for decimal numbers

Cevat :

Why does it give error?

>>> an = datetime.datetime.now(); an
datetime.datetime(2020, 3, 8, 22, 52, 19, 256487)
>>> datetime.datetime.strftime(an, '%-d')
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    datetime.datetime.strftime(an, '%-d')
ValueError: Invalid format string

Based on this sources (sources1, sources2) I expected "8"

zvone :

The page you linked says:

The Python docs contain all the format codes that the C standard (1989 version) requires, and these work on all platforms with a standard C implementation. Note that the 1999 version of the C standard added additional format codes. These include codes for non-zero-padded numbers, that can be obtained by appending a dash (-) (UNIX) or hash (#) (Windows) after the percent (%) sign.

So, this works on Linux:

datetime.datetime.strftime(an, '%-d')

and this works on Windows:

datetime.datetime.strftime(an, '%#d')

Interestingly enough, I see no mention of either of them in Python strftime docs. The whole feature may as well be unintentional in Python.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169842&siteId=1