python判断今天周几_Python如何根据日期判断周几
时间: 2023-09-27 12:04:45 浏览: 110
使用python计算指定天数是周几
可以使用Python中的datetime模块来判断日期是星期几。具体步骤如下:
1. 导入datetime模块
```python
import datetime
```
2. 获取当前日期
```python
today = datetime.date.today()
```
3. 判断星期几
```python
week_day = today.weekday() # 返回值为0-6,分别表示星期一到星期天
```
4. 将数字转换为星期几
```python
week_day_dict = {
0: '星期一',
1: '星期二',
2: '星期三',
3: '星期四',
4: '星期五',
5: '星期六',
6: '星期日'
}
week_day_str = week_day_dict.get(week_day)
```
完整代码如下:
```python
import datetime
today = datetime.date.today()
week_day = today.weekday()
week_day_dict = {
0: '星期一',
1: '星期二',
2: '星期三',
3: '星期四',
4: '星期五',
5: '星期六',
6: '星期日'
}
week_day_str = week_day_dict.get(week_day)
print('今天是:', week_day_str)
```
阅读全文