-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
  • 852
  • 4
  • 13
  • 34

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
  • 10,193
  • 7
  • 33
  • 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
  • 2,039
  • 17
  • 24