-4

I'm new to Python and I've searched around for the answer to this. I've seen similar questions but haven't been able to find exactly what I'm looking for.

I have a list of string dates in the format 'YYYY-mm-dd' that I would like to convert to a list of float dates in the format 'YYYYmmdd'. Any help here would be greatly appreciated.

Thanks

smalas
  • 29
  • 9
  • 3
    Start by breaking down the problem. In this case it can be broken down into 2 parts. **1**: [Convert from `YYYY-mm-dd` to `YYYYmmdd`](https://stackoverflow.com/a/502738/10400050). **2**: [Apply function to each element of a list](https://stackoverflow.com/a/25082439/10400050). When being a developer you'll never find any solution that fits your need to 100%. Instead you need to identify the different parts of your problem and solve them one by one. – Johan Nov 05 '18 at 10:32
  • 1
    You need to share the code you have tried so far – Satevg Nov 05 '18 at 10:32
  • Hints: `str.replace()` and `float()` are your friends. – bruno desthuilliers Nov 05 '18 at 10:33

3 Answers3

1
dates = ['2000-01-01', '2018-11-05']
[int(date.replace('-','')) for date in dates]
[20000101, 20181105]

You can use float instead of int, but I think there is no reason for that.

Andreas K.
  • 9,282
  • 3
  • 40
  • 45
0

You can use the time module

>>> import time
>>> from_date = "2016-09-23"
>>> to_date = time.strptime(from_date,"%Y-%m-%d")
>>> time.strftime("%Y%m%d",to_date)
'20160923'
Aravind
  • 534
  • 1
  • 6
  • 18
0

You can use the below code for iteration of list:

from datetime import datetime

mylist = ["05/12/2017","06/12/2017","23/05/2018"]
list2=[]

for x in mylist:
    final_date = datetime.strptime("21/12/2008", "%d/%m/%Y").strftime('%Y%m%d')
    list2.append(final_date)
print(list2)
pavan kumar
  • 102
  • 1
  • 2
  • 11