The code below should be back last Friday at 4:00 p.m. But he returns Friday of the previous week . How to fix it?
now = datetime.datetime.now() test = (now - datetime.timedelta(days=now.weekday()) + timedelta(days=4, weeks=-1)) test = test.replace(hour=16,minute=0,second=0,microsecond=0)
Upd. Now I use the following approach: is it the best?
now = datetime.datetime.now() if datetime.datetime.now().weekday() > 4: test = (now - datetime.timedelta(days=now.weekday()) + timedelta(days=4)) else: test = (now - datetime.timedelta(days=now.weekday()) + timedelta(days=4, weeks=-1)) test = test.replace(hour=16,minute=0,second=0,microsecond=0)
UPD2. Just to give an example. Suppose today is October 5, 2012. If the current time is equal to or less than 16:00, it must be returned on September 28, 2012, otherwise - on October 5, 2012.
source share