In the examples below, dis an object datetime.date.
To get the "index" of the day in the current month, use
def weekday_index(d):
return (d.day + 6) // 7
This formula will work no matter what day of the week it really is. To get a day that is the same day of the week with the same weekday index for the next month, the easiest way is
d_next = d + datetime.timedelta(weeks=4)
if weekday_index(d_next) < weekday_index(d):
d_next += datetime.timedelta(weeks=1)
, , , 4 5 d.