[Work Summary] Python获取每月特定日期

在最近的开发需求中涉及到获取特定日期(年月日格式)

例如,2nd week of July---获取2022年7月份第二周的日期(7月第2个星期一)

[Work Summary] Python获取每月特定日期_第1张图片

import calendar
c = calendar.Calendar(firstweekday=calendar.SUNDAY)
year = 2022
month = 7
monthcal = c.monthdatescalendar(year,month)
# sencond_monday返回date类型——2022-07-11
second_monday = [day for week in monthcal for day in week if \
                 day.weekday() == calendar.MONDAY and \
                 day.month == month][1]

second_monday:2022-07-11

你可能感兴趣的:(Work,Summary,python)