Home > Enterprise >  calculate days between several dates in python
calculate days between several dates in python

Time:11-01

I have a file with a thousand lines. There's 12 different dates in a single row.

I'm looking for two conditions. First: It should analyze row by row. For every row, it should check only for the dates between >=2022-12-1 & <=2024-12-31. Then, it should give me how many there is, BUT, with another specific condition.

for example, if there is: 2023-01-01, 2023-01-02, 2024-01-01, 2019-01-01, It should not give me 3 as an answer, but it should give me 2.

Because, it should calculate how many days there is between every of these dates, and if there is <90 days between one or another, it should "merge" them into one.

so for another example, if I have a set of : '2023-01-01, 2023-10-01, 2024-10-01' I should get 3 as a result.

... I know it's kind of messy, but I would really be glad to get some help.

EDIT:

I made a mistake in my original post. There is indeed 12 dates in every row. But they should be grouped by 2. So the first one with the second, the third one with the fourth, the fifth with the sixth and so on..

So let's take my first example :

2023-01-01, 2023-01-02, 2024-01-01, 2019-01-01.

It should calculate the difference between each one of these groups, so 2023-01-01 with 2023-01-02, AND, 2024-01-01 with 2019-01-01. Anytime there is 2 dates that are <90 days apart, it should count them as 1. If they are >90 days apart, it should count them as 2. So If I calculate the difference between 2023-01-01 and 2023-01-02, it gives me <90 days. So it's 1. Then i calculate 2024-01-01 with 2019-01-01, but since 2019-01-01 doesn't fit the first condtition, it should be ignored and count 2024-01-01 as one. Finaly it should add the first answer to the second, and give me the final answer, which is 2.

If instead 2019-01-01, I had 2024-05-01, I should give me 2 as the answer for that group since 2024-01-01 - 2024-05-01 = >90 days. Then finaly I would add 1 2 and the final answer would be 3.

CodePudding user response:

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
  • Related