Использование weekday
:
from datetime import datetime
weekday_map = {0: 'Monday',
1: 'Tuesday',
2: 'Wednesday',
3: 'Thursday',
4: 'Friday',
5: 'Saturday',
6: 'Sunday'}
suffix_map = {1: 'st',
2: 'nd',
3: 'rd',
4: 'th',
5: 'th'}
def which_weekday(date):
dt = datetime.strptime(date, '%Y-%m-%d')
which = (dt.day - 1) // 7 + 1
weekday = weekday_map[dt.weekday()]
return f'{which}{suffix_map[which]} {weekday} of the month'
print(which_weekday('2017-01-04'))
print(which_weekday('2017-01-25'))
print(which_weekday('2018-10-18'))
Вывод:
1st Wednesday of the month
4th Wednesday of the month
3rd Thursday of the month