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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With