0

I have a date in string format:

extractdate = '20200331'

I want to convert this to "31 Mar 2020".

I have the below so far but always get confused with the datetime operation:

(str(datetime.strptime(extractdate,'%Y%m%d').day) + " "
 + str(datetime.strptime(extractdate,'%Y%m%d').month) + " "
 + str(datetime.strptime(extractdate,'%Y%m%d').year))

which gives me

'31 3 2020'

How do I convert 3 to Mar?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Use %b, e.g. `datetime.strptime('20200331','%Y%m%d').strftime('%d %b %Y')`. Checkout https://strftime.org/, takes you less time than you need to write a question on SO ;-) – FObersteiner Jun 03 '20 at 13:56

1 Answers1

1

Use %B for March or %b for the abbreviated Mar. You can find all the cases here.

datetime.strptime('20200331','%Y%m%d').strftime('%d %b %Y')
alec_djinn
  • 10,104
  • 8
  • 46
  • 71