On SO question 904928 (Python strftime - date without leading 0?) Ryan answered:
Actually I had the same problem and I realised that, if you add a
hyphen between the % and the letter, you can remove the leading zero.
For example %Y/%-m/%-d.
I faced the same problem and that was a great solution, BUT, why does this behave like this?
>>> import datetime
>>> datetime.datetime(2015, 3, 5).strftime('%d')
'05'
>>> datetime.datetime(2015, 3, 5).strftime('%-d')
'5'
# It also works with a leading space
>>> datetime.datetime(2015, 3, 5).strftime('%e')
' 5'
>>> datetime.datetime(2015, 3, 5).strftime('%-e')
'5'
# Of course other numbers doesn't get stripped
>>> datetime.datetime(2015, 3, 15).strftime('%-e')
'15'
I cannot find any documentation about that? -> python datetime docs / python string operations
It seems like this doesn't work on windows machines, well I don't use windows but it would be interesting to know why it doesn't work?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…