How to calculate next Friday at 3am?
- by Mark
How can you calculate the following Friday at 3am as a datetime object?
Clarification: i.e., the calculated date should always be greater than 7 days away, and less than or equal to 14.
Going with a slightly modified version of Mark's solution:
def next_weekday(dt=datetime.datetime.now(), time_of_day=datetime.time(hour=3), day_of_week=4):
dt += datetime.timedelta(days=7)
if dt.time() < time_of_day: dt = dt.combine(dt.date(), time_of_day)
else: dt = dt.combine(dt.date(), time_of_day) + datetime.timedelta(days=1)
return dt + datetime.timedelta((day_of_week - dt.weekday()) % 7)