Look at the types of these properties:
In [1]: import datetime In [2]: d = datetime.date.today() In [3]: type(d.month) Out[3]: <type 'int'> In [4]: type(d.day) Out[4]: <type 'int'>
Both are integers. Thus, there is no automatic way to do what you want. Therefore, in a narrow sense, the answer to your question is no .
If you need leading zeros, you will have to format them anyway. To do this, you have several options:
In [5]: '{:02d}'.format(d.month) Out[5]: '03' In [6]: '%02d' % d.month Out[6]: '03' In [7]: d.strftime('%m') Out[7]: '03'
Roland Smith Mar 19 '13 at 20:24 2013-03-19 20:24
source share