def picknthweekday(year, month, dayofweek, hour, minute, whichweek):
 first = datetime.datetime(year, month, 1, hour, minute)
 weekdayone = first.replace(day=(((dayofweek - first.isoweekday()) % 7) + 1))
 wd = (weekdayone + ((whichweek - 1) * ONEWEEK))
 if (wd.month != month):
  wd -= ONEWEEK
 return wd

def picknthweekday ( year , month , dayofweek , hour , minute , whichweek ) : first = datetime . datetime ( year , month , 1 , hour , minute ) weekdayone = first . replace ( day = ( ( ( dayofweek - first . isoweekday ( ) ) % 7 ) + 1 ) ) wd = ( weekdayone + ( ( whichweek - 1 ) * ONEWEEK ) ) if ( wd . month != month ) : wd -= ONEWEEK return wd
dayofweek == 0 means sunday .