Convert date with format dd.mm.yy to yyyy-mm-dd in python
I have a date with the format:
d1 = "22.05.15"
I want to change the format to date:
d1 = "2015-05-22"
I tried to convert using datetime:
d2 = datetime.strptime(d1,'%d.%m.%Y').strftime('%Y-%d-%m')
But that doesn't work because datetime doesn't support date formatted as '22 .05.15 ', but only '22 .05.2015'
Is there any way out for converting a date to this format?
+3
blues
source
to share
1 answer
Use %y
instead %y
:
>>> d2 = datetime.datetime.strptime(d1,'%d.%m.%y').strftime('%Y-%m-%d')
>>> d2
'2015-05-22'
+4
Simeon visser
source
to share