-3

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

Michael Ruth
  • 2,938
  • 1
  • 20
  • 27
jay
  • 3
  • 3
  • 2
    Please review the question. There are multiple output values specified (`2014-06-09 23:11:00`, `2014-04-01 00:11:00`) and neither can be achieved by reformatting the input value `09-JUN-14 12.00.00.000000000 AM`. – Michael Ruth Aug 11 '23 at 17:26
  • You also have some basic mistakes, like to start, `F` instead of `%f` (although `datetime` only handles up to microseconds, so it doesn't work) and no format code for "AM" in the first one. – wjandrea Aug 11 '23 at 17:40

1 Answers1

1

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:

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Grady Ela
  • 46
  • 1
  • 4
  • 1
    This'll only work if the nanoseconds are always 000. Pandas, on the other hand, supports nanoseconds. – wjandrea Aug 11 '23 at 17:49