关于获取每个月第几周的第一天是周几和最后一天是几号 python

2022-09-02 18:21:10 浏览数 (1)

代码语言:javascript复制
import calendar

dict_data = {
    1: "一",
    2: "二",
    3: "三",
    4: "四",
    5: "五",
    6: "六",
    7: "日"
}


# 获取第一周的第一天是周几和最后一天是周几
def get_weekday(_mouth, _index=1):
    start = ''
    count = 0
    for i in _mouth[_index - 1]:
        if i != 0:
            start = count   i
            break
        count  = 1
    # 解决最后是0的情况
    end = _mouth[_index - 1][-1]
    if end == 0:
        for i in _mouth[_index - 1][::-1]:
            if i != 0:
                end = i
                break
    # 解决第几周第一天的下标
    print("第一周的第一天是周{},最后一天是{}号".format(dict_data[_mouth[_index - 1].index(start)   1], str(end)))



# 创建一个日历
c = calendar.TextCalendar(calendar.MONDAY)
# 输出某年某月全部日期
mouth = c.monthdayscalendar(2021, 9)
print(mouth)
get_weekday(mouth)

output:

代码语言:javascript复制
[[0, 0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26], [27, 28, 29, 30, 0, 0, 0]]
第一周的第一天是周三,最后一天是5号

0 人点赞