python datetime获取一个星期的日历
时间: 2023-05-13 09:05:04 浏览: 103
Python Datetime模块和Calendar模块用法实例分析
可以使用Python的calendar模块来获取一个星期的日历。以下是一个示例代码:
```python
import calendar
import datetime
# 获取当前日期
today = datetime.date.today()
# 获取本周第一天的日期
start_of_week = today - datetime.timedelta(days=today.weekday())
# 获取本周最后一天的日期
end_of_week = start_of_week + datetime.timedelta(days=6)
# 获取本周日历
calendar_week = calendar.monthcalendar(today.year, today.month)
week = [day[start_of_week.weekday()] for day in calendar_week]
# 打印本周日历
print(week)
```
这段代码会输出本周的日期列表,例如:
```
[datetime.date(2021, 11, 8), datetime.date(2021, 11, 9), datetime.date(2021, 11, 10), datetime.date(2021, 11, 11), datetime.date(2021, 11, 12), datetime.date(2021, 11, 13), datetime.date(2021, 11, 14)]
```
阅读全文