How to calculate next Friday at 3am?
Posted
by Mark
on Stack Overflow
See other posts from Stack Overflow
or by Mark
Published on 2010-03-13T01:02:16Z
Indexed on
2010/03/13
9:05 UTC
Read the original article
Hit count: 372
python
|date-arithmetic
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)
© Stack Overflow or respective owner