Python datetime formatting without zero-padding

The other alternate to avoid the “all or none” leading zero aspect above is to place a minus in front of the field type:

mydatetime.strftime('%-m/%d/%Y %-I:%M%p')

Then this: ‘4/10/2015 03:00AM’

Becomes: ‘4/10/2015 3:00AM’

You can optionally place a minus in front of the day if desired.

Edit: The minus feature derives from the GNU C library (“glibc”) as mentioned in the Linux strftime manpage under “Glibc notes”

Leave a Comment