X
X
Xaip2018-04-10 15:21:47
Python
Xaip, 2018-04-10 15:21:47

How to increment date in python?

I am using the datetime library in python. And I need to clear some files on a certain day of the week. I solved this problem by calculating the delta and incrementing the day of the week. But this method does not work due to the fact that the month and year are not updated. Tell me the best way to make a timer

def get_delta(self):
        c1 = datetime.datetime.today()
        now_time = datetime.date(c1.year, c1.month, c1.day)
        wipe = datetime.date(self.date_to_wipe[0],self.day_wipe,self.date_to_wipe[2])
        delta = wipe - now_time
        return delta
    def run(self):
        while 1:

            delta = self.get_delta()
            if delta.days <= 0:
                self.thread_lock.acquire(1)
                self.kill_dedicated_server()
                self.remove_files()
                self.open_dedicated_server()
                self.day_wipe += 7
                self.thread_lock.release()
            else:
                print(self.name_wipe + ' : '+ delta)
            time.sleep(self.sleep_time)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rostislav Grigoriev, 2018-04-10
@crazyzubr

How to increment date in python?

use time delta
from datetime import datetime, timedelta

some_date = datetime(year=2018, month=10, day=30)
delta_some_date = some_date + timedelta(days=5)
print(delta_some_date)

Output :
2018-11-04 00:00:00

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question