DotBotHCV
DotBotHCV

Reputation: 33

How to loop a program and increase the date by 1 on each next loop?

I want to change the following program to print all(infinite) dates from a given date specified by def increase_by_one(self): but I don't know where to begin with. :( And I want to start from a specific starting date, no need to start from 01-01-0001

    def date(self):
        var_date = (datetime.datetime.now()+timedelta(%s)).strftime('%Y%m%d') % self.increase_by_one()
        return var_date
    def print(self):
        x= self.date()
        print(x)

Upvotes: 0

Views: 293

Answers (1)

blueteeth
blueteeth

Reputation: 3555

If you want an infinite stream of dates, you could use a generator.

from datetime import datetime, timedelta

def gen_dates(start_date):
    one_day = timedelta(days=1)
    while True:
        yield start_date
        start_date += one_day

You can use it like this:

In [3]: g = gen_dates(datetime.now())

In [4]: next(g)
Out[4]: datetime.datetime(2019, 11, 19, 15, 3, 21, 102090)

In [5]: next(g)
Out[5]: datetime.datetime(2019, 11, 20, 15, 3, 21, 102090)

...

Upvotes: 2

Related Questions