The code below should return last Friday, 16:00:00. But it returns Friday of previous week. How to fix that?
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. I use the following approach now - is it the best one?
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. Let's assume that today is Oct 5, 2012. In case current time is equal to or less than 16:00 it should return Sep 28, 2012, otherwise - Oct 5, 2012.