Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44222 > unrolled thread

Re: datetime.strptime() not padding 0's

Started byKyle Shannon <kyle@pobox.com>
First post2013-04-23 15:44 -0600
Last post2013-04-23 15:44 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: datetime.strptime() not padding 0's Kyle Shannon <kyle@pobox.com> - 2013-04-23 15:44 -0600

#44222 — Re: datetime.strptime() not padding 0's

FromKyle Shannon <kyle@pobox.com>
Date2013-04-23 15:44 -0600
SubjectRe: datetime.strptime() not padding 0's
Message-ID<mailman.999.1366753466.3114.python-list@python.org>
On Tue, Apr 23, 2013 at 3:41 PM, Kyle Shannon <kyle@pobox.com> wrote:
> On Tue, Apr 23, 2013 at 3:14 PM, Rodrick Brown <rodrick.brown@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'


Whoops, wrong cut/paste, I meant:

>>> dt.strftime('%b %d %Y')
'Apr 09 2013'

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web