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
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
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.
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')