.replace()
does not accept regular expressions. You are trying to replace literal text ^0
.
Use str.format()
to create a date format without zero padding:
'{0.month}/{0.day}/{0.year}'.format(my_date)
and no need to replace zeros.
Demo:
>>> import datetime >>> today = datetime.date.today() >>> '{0.month}/{0.day}/{0.year}'.format(today) '9/10/2013'
If Python was compiled using the glibc
library, you can also use dashes in the format to suppress padding:
my_date.strftime('%-m/%-d/%y')
but it is not so portable.
source share