pythondatetimestrptime

have issue converting '09-JUN-14 12.00.00.000000000 AM' to datetime in python


I have been trying to convert '09-JUN-14 12.00.00.000000000 AM' to '2014-06-09 23:11:00'

I have tried two methods:

dk = datetime.strptime( '09-JUN-14 12.00.00.000000000 AM', '%D-%M-%Y %H.%M.%S.F')

dk = pd.to_datetime('09-JUN-14 12.00.00.000000000 AM','%DD-%MON-%RR %HH.%MI.%SS.%FF %AM')

I'm not able to convert it to date time

I wanted to have in format like

2014-04-01 00:11:00


Solution

  • You'll want something like this:

    import datetime
    
    a = datetime.datetime.strptime('09-JUN-14 12.00.00.000000000 AM', '%d-%b-%y %I.%M.%S.%f000 %p')
    print(a)
    

    Result: 2014-06-09 00:00:00

    With datetime parsing, you've just gotta do trial and error until it starts parsing stuff correctly. And you have to read the manual. Abbreviated month is different from numeric month. Just like 2 digit year is different than 4 digit year. Milliseconds expects 6 digits, so you have to add the trailing 000 for your example.

    A quick google and you'll pull up 4 different websites with quick how-tos and summary docs on strptime. For example: