import calendar
from datetime import datetime
def count_specific_weekdays(year=None, month=None):
"""
计算指定年月中周二、周四和周六的总天数
参数:
year (int): 指定年份,默认为当前年份
month (int): 指定月份,默认为当前月份
返回:
int: 周二、周四和周六的天数总和
"""
if year is None or month is None:
now = datetime.now()
year, month = now.year, now.month
# 获取当月的日历矩阵
cal = calendar.monthcalendar(year, month)
# 统计每周中周二(1)、周四(3)、周六(5)的出现次数
count = 0
for week in cal:
count += sum(1 for day in [1, 3, 5] if week[day] != 0)
return count
# 示例:获取当前月份的天数总和并打印
if __name__ == "__main__":
current_month = datetime.now().month
current_year = datetime.now().year
total_days = count_specific_weekdays(current_year, current_month)
print(total_days)