-2

I have date strings similar to type of the following format,

7-Dec-16

How can I convert it to the format given below using python??

2016-12-07

Vajira Prabuddhaka
  • 744
  • 2
  • 11
  • 29

2 Answers2

2

You can use strptime from datetime

from datetime import datetime

datetime_formatted = datetime.strptime('7-Dec-16', '%d-%b-%y')

print (datetime_formatted)

You can see in here for the usage of all directives.

%b Month as locale’s abbreviated name.

%d Day of the month as a zero-padded decimal number.

%y Year without century as a zero-padded decimal number.

omri_saadon
  • 9,513
  • 6
  • 31
  • 58
1
import dateparser
dateparser.parse('7-Dec-16').strftime('%d-%m-%Y')

Output : '07-12-2016'

you can use dateparser library for multiple function. link

For your case you can get accurate answer by:

dateparser.parse('7-Dec-16').strftime('%Y-%m-%d')

Rohit-Pandey
  • 1,814
  • 15
  • 24