获取一年中的自然周

2025-11-15 08:42:35

自然周:以每月第一个星期一作为这个月第一周的开始。前面不满星期一的作废(其实它是算在上一个月最后一周的。观察日历即可明白)

import datetime

import calendar

def out_day_by_date(date):

'''

根据输入的日期计算该日期是在当年的第几天

'''

year=date.year

month=date.month

day=date.day

months=[0,31,59,90,120,151,181,212,243,273,304,334]

if 0

sum=months[month-1]

else:

print("month error")

sum+=day

leap=0

#接下来判断平年闰年

if(year%400==0) or ((year%4)==0) and (year%100!=0):#and的优先级大于or

#1、世纪闰年:能被400整除的为世纪闰年

#2、普通闰年:能被4整除但不能被100整除的年份为普通闰年

leap=1

if(leap==1) and (month>2):

sum+=1#判断输入年的如果是闰年,且输入的月大于2月,则该年总天数加1

return sum

def week_to_month(years):

'''

返回一个数组,索引为第几周(从1开始),值为所属的月份

Args:

years: 所使用的的年份

Returns:

week_to_month: 周转月份数组

'''

week_to_month = [0]

day = 1

j = 0

if (years % 4 == 0 and years % 100 != 0) or (years % 400 == 0):

max_day = 366 + 1

else:

max_day = 365 + 1

while day < max_day:

first_day = datetime.datetime(years, 1, 1)

add_day = datetime.timedelta(days=day - 1)

# 当前是哪天

current_day = first_day + add_day

# 当前这一天是周几

current_weekday = get_week_day(current_day)

# 如果当前这一天不是周一,则加到周一

if current_weekday != 1:

day += 7 - current_weekday + 1

else:

week_to_month.append(current_day.month)

j += 1

print(current_day, j)

day = min(day + 7, out_day_by_date(datetime.datetime(years, current_day.month, 1)) +

calendar.monthrange(years, current_day.month)[1])

return week_to_month

def month_to_week(years):

w_t_m = week_to_month(years)

w_t_m = enumerate(w_t_m)

month_to_week = {}

for week, month in w_t_m:

if str(month) in month_to_week.keys():

month_to_week[str(month)].append(week)

else:

month_to_week[str(month)] = [week]

print(month_to_week)

def get_week_day(date):

week_day_dict = {

0 : 1,

1 : 2,

2 : 3,

3 : 4,

4 : 5,

5 : 6,

6 : 7,

}

day = date.weekday()

return week_day_dict[day]

if __name__ == '__main__':

w_t_m = week_to_month(2019)

print(w_t_m)

结果:

2019-01-07 00:00:00 1

2019-01-14 00:00:00 2

2019-01-21 00:00:00 3

2019-01-28 00:00:00 4

2019-02-04 00:00:00 5

2019-02-11 00:00:00 6

2019-02-18 00:00:00 7

2019-02-25 00:00:00 8

2019-03-04 00:00:00 9

2019-03-11 00:00:00 10

2019-03-18 00:00:00 11

2019-03-25 00:00:00 12

2019-04-01 00:00:00 13

2019-04-08 00:00:00 14

2019-04-15 00:00:00 15

2019-04-22 00:00:00 16

2019-04-29 00:00:00 17

2019-05-06 00:00:00 18

2019-05-13 00:00:00 19

2019-05-20 00:00:00 20

2019-05-27 00:00:00 21

2019-06-03 00:00:00 22

2019-06-10 00:00:00 23

2019-06-17 00:00:00 24

2019-06-24 00:00:00 25

2019-07-01 00:00:00 26

2019-07-08 00:00:00 27

2019-07-15 00:00:00 28

2019-07-22 00:00:00 29

2019-07-29 00:00:00 30

2019-08-05 00:00:00 31

2019-08-12 00:00:00 32

2019-08-19 00:00:00 33

2019-08-26 00:00:00 34

2019-09-02 00:00:00 35

2019-09-09 00:00:00 36

2019-09-16 00:00:00 37

2019-09-23 00:00:00 38

2019-09-30 00:00:00 39

2019-10-07 00:00:00 40

2019-10-14 00:00:00 41

2019-10-21 00:00:00 42

2019-10-28 00:00:00 43

2019-11-04 00:00:00 44

2019-11-11 00:00:00 45

2019-11-18 00:00:00 46

2019-11-25 00:00:00 47

2019-12-02 00:00:00 48

2019-12-09 00:00:00 49

2019-12-16 00:00:00 50

2019-12-23 00:00:00 51

2019-12-30 00:00:00 52

[0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12]