Python datetime time format removes the leading 0

  • 2020-04-02 13:57:11
  • OfStack

When formatting Python time, remove the leading zero:


dt = datetime.now() 
print dt.strftime('%-H')

# result: '4'

Just add a "-" between the % and flag of format string.

It has been tried on Mac and Linux and is not sure if it is portable.


Related articles: