Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i convert different date python format? [duplicate]

Tags:

python

date

I have dd-MMM-yyyy dates. How do i convert this to yyyyMMdd, in Python ?

For example i need to convert 20-Nov-2002 to 20021120

like image 885
Pedro Sousa Avatar asked Sep 17 '15 01:09

Pedro Sousa


1 Answers

You can use datetime.datetime.strptime() to read the date in a specific format and then use .strftime() to write it back in your required format. Example -

>>> import datetime
>>> datetime.datetime.strptime('20-Nov-2002','%d-%b-%Y').strftime('%Y%m%d')
'20021120'

Formats -

%d - 2 digit date

%b - 3-letter month abbreviation

%Y - 4 digit year

%m - 2 digit month

More details about different supported formats can be found here.

like image 74
Anand S Kumar Avatar answered Sep 21 '22 10:09

Anand S Kumar