0

I would need to iterate one day a time the following functions which contains this parameter as follows:

start_date=input("\nStart date: mm/dd/yyyy ")
end_date=start_date

fun(start_date,end_date)

where start_date should be a date chosen by user in that format. End_date has to be equal to start date, as I am interested in one-day search. However, I would need to repeat this research also for the other days. My code is currently doing the following:

  • enter a start_date. It is my start date. For example: 08/22/2020
  • then set end_date equal to start_date. Then it is equal to 08/22/2020
  • apply this function to look for information in 08/22/2020;
  • when it finishes (it is included in my function), please update the date, increasing by one (i.e. 08/23/2020);
  • repeat the search in 08/23/2020

I am having difficulties to look for next day, also because there might be months with 30, 31 and 28/29 days.

Could you please tell me how could I update my code?

Almog
  • 452
  • 1
  • 7
  • 13
  • 2
    Refer to [How to increment a datetime by one day?](https://stackoverflow.com/questions/3240458/how-to-increment-a-datetime-by-one-day). – Aviv Yaniv Aug 22 '20 at 22:24
  • Seems that the question in principle is asking for calculation using strings. But really, you ought to convert your dates into `datetime.datetime` objects immediately after reading in (see `datetime.datetime.strptime`), do all the manipulation in that form, and only convert back to strings when needed for output purposes (see `strftime` method). – alani Aug 22 '20 at 22:39
  • Does this answer your question? [How to increment a datetime by one day?](https://stackoverflow.com/questions/3240458/how-to-increment-a-datetime-by-one-day) – Andreas Aug 22 '20 at 22:40
  • Specifically to convert to datetime: `dt = datetime.datetime.strptime('08/22/2020', '%m/%d/%Y')`, to convert back again: `dt.strftime('%m/%d/%Y')`, and to do the calculation, see the link that a couple of people have posted. – alani Aug 22 '20 at 22:42
  • 1
    Refer timedelta class. Its sole purpose is to add, subtract days/h/m/s to datetime objects. – Abhilash Aug 22 '20 at 23:06
  • Use a [`datetime.timedelta`](https://docs.python.org/3/library/datetime.html#datetime.timedelta). – martineau Aug 22 '20 at 23:54

1 Answers1

0

This might help:

for n in range(int((end_date - start_date).days)):
        yield start_date + timedelta(n)

i am using generator to hide/abstract iteration over the range of dates.

ghazigamer
  • 100
  • 9