例如,我们有
date = 29
number_of_week = 52
year = 2012
如何计算月数?本例中为 12。谢谢
from datetime import date, timedelta
def get_month(year, week, day):
january_first = date(year, 1, 1)
monday_date = january_first + timedelta(days=week * 7 - january_first.weekday())
for d in range(7):
weekday_date = monday_date + timedelta(days=d)
if weekday_date.day == day:
return weekday_date.month
在这一点上,很明显你需要更好的设计。更大的图景是什么?为什么你需要这一切?