0

I have following while loop in Python:

start_date: datetime.date = utcnow().date() - datetime.timedelta(
    days=30
)
end_date: datetime.date = utcnow().date()
DELTA_1D: datetime.timedelta = datetime.timedelta(days=1)
while start_date <= end_date:
    # some code
    start_date += DELTA_1D

But I want to transform it to a for loop. What's the best implementation for this?

1 Answer 1

1

Work out he number of days between by simply subtracting

from datetime import date, timedelta

d0 = date(2008, 8, 18)
d1 = date(2008, 9, 26)
delta = d1 - d0
for i in range(0,delta.days):
        date_to_handle = d0 + timedelta(days=i)
Sign up to request clarification or add additional context in comments.

5 Comments

I want to know which date I handle so this is a bit to complex than
Do you want a for each loop?
I want to loop over each day between those 2 intervals and know which date I'm handling
Updated so that imports are included and you have the date within the for loop
You can do this with date or datetiem @sg_sg94

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.