Python converts one digit day and month to two digits

I am trying to read and convert the following datetime "6/5/2014 00:09:32"

to "Mon Mar 09 07:07:18 +0000 2015"

.

d = datetime.datetime.strptime('6/5/2014 00:09:32', '%d/%m/%y %H:%M:%S')

      

Gives me a traceback with:

ValueError: time data '6/5/2014 00:00:07' does not match format '%d/%m/%y %H:%M:%S'

      

I tried using% e for single digits as suggested here: http://www.gnu.org/software/libc/manual/html_node/Formatting-Calendar-Time.html#index-strftime-2660 but datetime just says, which does not recognize e.

If I'm missing something, it looks like one-digit days and months, this is my problem, but from the documentation ( https://docs.python.org/2/library/time.html#time.strftime ) 't see a way get python datetime to read single digit days or months. But that seems silly. What am I missing?

Thank you for your help.

+3


source to share


1 answer


You do 'Y'

n't need 'Y'

:

In [412]:

d = dt.datetime.strptime('6/5/2014 00:09:32', '%d/%m/%Y %H:%M:%S')
d
Out[412]:
datetime.datetime(2014, 5, 6, 0, 9, 32)

      

The docs 'Y'

correspond to:



Year with century as a decimal number.

Have you tried 'Y'

which:

Year without century as a decimal number [00.99].

+6


source







All Articles