(year, month, dayofweek, hour, minute, whichweek)
dayofweek == 0 means Sunday, whichweek > 4 means last instance
dayofweek == 0 means Sunday, whichweek > 4 means last instance
dayofweek == 0 means Sunday whichweek > 4 means last instance
def pickNthWeekday(year, month, dayofweek, hour, minute, whichweek):
    """dayofweek == 0 means Sunday, whichweek > 4 means last instance"""
    first = datetime.datetime(year=year, month=month, hour=hour, minute=minute,
                              day=1)
    weekdayone = first.replace(day=((dayofweek - first.isoweekday()) % 7 + 1))
    for n in xrange(whichweek - 1, -1, -1):
        dt=weekdayone + n * WEEKS
        if dt.month == month: return dt
def pickNthWeekday ( year , month , dayofweek , hour , minute , whichweek ) : first = datetime . datetime ( year = year , month = month , hour = hour , minute = minute , day = 1 ) weekdayone = first . replace ( day = ( ( dayofweek - first . isoweekday ( ) ) % 7 + 1 ) ) for n in xrange ( whichweek - 1 , - 1 , - 1 ) : dt = weekdayone + n * WEEKS if dt . month == month : return dt