One way to solve it would be the one below (Keep in my that this is a minimum example, describing the logic):
from datetime import datetime
line = "2023-01-01, 2023-01-02, 2024-01-01, 2019-01-01"
# Split and convert strings to datetime objects
dates = list(datetime.strptime(elem, '%Y-%m-%d') for elem in line.split(', '))
total = 0
# Read pairs of dates and decide whether they should be merged or not.
for i in range(0, len(dates), 2):
delta = dates[i+1] - dates[i]
if delta.days > 90:
total += 2
else:
total += 1
print(total)
This will return:
2
You can also create a function for that and use it for each row:
from datetime import datetime
def getTotal(line):
dates = list(datetime.strptime(elem, '%Y-%m-%d') for elem in line.split(', '))
total = 0
for i in range(0, len(dates), 2):
delta = dates[i+1] - dates[i]
if delta.days > 90:
total += 2
else:
total += 1
return total
line = "2023-01-01, 2023-01-02, 2024-01-01, 2019-01-01"
anotherLine = "2023-01-01, 2023-01-02, 2024-01-01, 2024-05-01"
print(getTotal(line))
print(getTotal(anotherLine))
This will return:
2
3
5
solved calculate days between several dates in python