P
P
PILITYXXX1232020-08-30 20:27:23
Python
PILITYXXX123, 2020-08-30 20:27:23

How to change the type of dates in the list?

There is a list:

a = ['12.12.2020', '13.12.2020', '15.10.2020', '16.12.2020', '20.08.2020']

It is necessary to change the date type in each element of the list, that is, the result should be like this:

b = ['2020-12-12', '2020-12-13', '2020-10-15', '2020-12-16', '2020-08-20']

Answer the question

In order to leave comments, you need to log in

3 answer(s)
0
0xD34F, 2020-08-30
@0xD34F

[ '-'.join(reversed(n.split('.'))) for n in a ]

D
Dr. Bacon, 2020-08-30
@bacon

That's all the basics, let's do it yourself.
https://docs.python.org/3/library/datetime.html#da...
https://docs.python.org/3/library/datetime.html#da...
or just split by dot and reassemble

V
Vladimir Kuts, 2020-08-31
@fox_12

Option with intermediate conversion to date:

import datetime
b = list(map(lambda x: datetime.datetime.strptime(x, '%d.%m.%Y').strftime('%Y-%m-%d'), a))

['2020-12-12', '2020-12-13', '2020-10-15', '2020-12-16', '2020-08-20']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question