datetime.strptime() not padding 0's

Kyle Shannon kyle at pobox.com
Tue Apr 23 17:41:34 EDT 2013


On Tue, Apr 23, 2013 at 3:14 PM, Rodrick Brown <rodrick.brown at gmail.com> wrote:
> I thought I read some where that strptime() will pad 0's for day's for some
> reason this isnt working for me and I'm wondering if i'm doing something
> wrong.
>
>>>> from datetime import datetime
>>>> dt = datetime.strptime('Apr 9 2013', '%b %d %Y')
>>>> dt.day
> 9
>>>>
>
> How can I get strptime to run 09? instead of 9
>
>
> --RB
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

dt.day is an integer:

>>> from datetime import datetime
>>> dt = datetime.strptime('Apr 9 2013', '%b %d %Y')
>>> type(dt.day)
<type 'int'>

I think you are confusing strftime() with strptime():

>>> dt.strftime('%b %D %Y')
'Apr 04/09/13 2013'

or if you just want a 0 padded string for the day, use string formatting:

>>> s = '%02d' % dt.day
>>> type(s)
<type 'str'>
>>> s
'09'



More information about the Python-list mailing list