Вы можете использовать календарь:
import calendar
import numpy as np
calendar.setfirstweekday(6)
def get_week_of_month(year, month, day):
x = np.array(calendar.monthcalendar(year, month))
week_of_month = np.where(x==day)[0][0] + 1
return(week_of_month)
get_week_of_month(2019,12,31)
Вывод:
5
Вот что они используют:
def week_of_month(d):
first_day_of_month = d.replace(day=1)
print(d.week_of_year,first_day_of_month.week_of_year)
return d.week_of_year - first_day_of_month.week_of_year + 1
week_of_month(d)
Ошибка здесь
first_day_of_month.week_of_year gives --> 48
Раньше они использовали это:
import math
def week_of_month(d):
return int(math.ceil(d.day / 7))
, что дает правильный результат