You are using the wrong tool for your work, use strftime
>>> datetime.date(2002, 12,4).strftime("%Y%m%d") '20021204'
For more information on using strftime and strptime , consult strftime () and strptime () Behavior
In your particular case, I will give an appropriate passage
- % Y A year with a century as a decimal number. 1970, 1988, 2001, 2013.
- % m The month as a decimal number with zero margin. 01, 02, ..., 12
- % d Day of the month as a decimal with zero margin. 01, 02, ..., 31
you could always remove or replace the hyphen from isoformat
>>> str(datetime.date(2002, 12,4)).translate(None,'-') '20021204'
source share