ghickman ghickman - 1 year ago 154
Python Question

Python: Find the date for the first Monday after a given a date

Given a particular date, say 2011-07-02, how can I find the date of the next Monday (or any weekday day for that matter) after that date?

Answer Source
import datetime
def next_weekday(d, weekday):
    days_ahead = weekday - d.weekday()
    if days_ahead <= 0: # Target day already happened this week
        days_ahead += 7
    return d + datetime.timedelta(days_ahead)

d =, 7, 2)
next_monday = next_weekday(d, 0) # 0 = Monday, 1=Tuesday, 2=Wednesday...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download