mausasu1234
mausasu1234

Reputation: 37

python loop between start data and end date

I have problem with making correct loop from start date and end date.

Both of them I got from js in string format.

For example start date 2021-07-31 07:30:00

End game 2021-10-27

Then I convert both of them in datatime For example its how i convert End Date

 _endDate = datetime.strptime(_ruccerenceList[0], "%Y-%m-%d")  

In the loop with timedelta I wanna add days to Start date.

 +  dt.timedelta(days=int(_ruccerenceList[2])) 

_ruccerenceList[2] can be 1-99

So loop must end then Start date > End Date

What I try its look funny but first I try just do this

while _perData <= _endDate:

When try this way

                date_array = \
                    (datetime.strptime(date, "%Y-%m-%d %H:%M:%S") + dt.timedelta(days=x) for x in range(int(_ruccerenceList[2]), (_endDate-datetime.strptime(date, "%Y-%m-%d %H:%M:%S")).days))
                
                for date_object in date_array:


                    print(date_object.strftime("%Y-%m-%d"))

But its not working, if for example _ruccerenceList[2] is 3, then first loop working fine, but then it just going not per 3 days, but per 1 day.

Any idea how to solve it?

Upvotes: 0

Views: 671

Answers (2)

jthulhu
jthulhu

Reputation: 8678

Assuming you have three variables, start_string, end_string and delta_string which correspond to the data you have received. Then this should do what you want.

import datetime as dt
from datetime import datetime
FORMAT = "%Y-%m-%d"
start = datetime.strptime(start_string, FORMAT)
end = datetime.strptime(end_string, FORMAT)
delta = dt.timedelta(days=int(delta_string))
curr = start
while curr <= end:
  print(curr)
  curr += delta

With start_string="2021-07-31", end_string="2021-10-27" and delta_string="3", this gives the following output:

2021-07-31 00:00:00
2021-08-03 00:00:00
2021-08-06 00:00:00
2021-08-09 00:00:00
2021-08-12 00:00:00
2021-08-15 00:00:00
2021-08-18 00:00:00
2021-08-21 00:00:00
2021-08-24 00:00:00
2021-08-27 00:00:00
2021-08-30 00:00:00
2021-09-02 00:00:00
2021-09-05 00:00:00
2021-09-08 00:00:00
2021-09-11 00:00:00
2021-09-14 00:00:00
2021-09-17 00:00:00
2021-09-20 00:00:00
2021-09-23 00:00:00
2021-09-26 00:00:00
2021-09-29 00:00:00
2021-10-02 00:00:00
2021-10-05 00:00:00
2021-10-08 00:00:00
2021-10-11 00:00:00
2021-10-14 00:00:00
2021-10-17 00:00:00
2021-10-20 00:00:00
2021-10-23 00:00:00
2021-10-26 00:00:00

Upvotes: 1

Yevhen Kuzmovych
Yevhen Kuzmovych

Reputation: 12140

First idea: don't write long complicated lines of code. Separate things into meaningful variables. That would be much easier to debug, understand and also would work faster. Second, range iterates from start (first argument of range) to end (second argument) one by one. range also has the third argument step which you need to set to iterate with the given step:

start_date = datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
start = step = int(_ruccerenceList[2])
days = _endDate - datetime.strptime(date, "%Y-%m-%d %H:%M:%S")).days

date_array = (start_date + dt.timedelta(days=x) for x in range(start, days, step))

Upvotes: 1

Related Questions