下面的代码应该在上周五 16:00:00 返回。但它返回前一周的星期五。如何解决?
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)
更新。我现在使用以下方法 - 它是最好的吗?
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)
更新2。只是举个例子。假设今天是Oct 5, 2012。如果当前时间等于或小于16:00,则应返回Sep 28, 2012,否则 - Oct 5, 2012。