python 獲取當前月份
时间: 2023-10-17 17:27:12 浏览: 19
你可以使用Python的datetime模块来获取当前月份。以下是一个示例代码:
```python
from datetime import datetime
current_month = datetime.now().month
print("当前月份:", current_month)
```
运行此代码将输出当前月份的数字(1到12之间的整数)。请注意,此代码将在您的系统时区中获取当前日期和时间。
相关问题
python获取当前月份
您可以使用Python中的datetime模块来获取当前月份。下面是一个示例代码:
```python
import datetime
now = datetime.datetime.now()
month = now.month
print("当前月份为:", month)
```
输出结果类似于:
```
当前月份为: 8
```
python获取当前日期后输出月份
可以通过datetime模块来获取当前日期,然后从中提取出月份信息。具体的代码如下:
```python
import datetime
now = datetime.datetime.now()
month = now.month
print("当前月份为:", month)
```
输出结果类似于:
```
当前月份为: 5
```
如果你想要输出中文的月份名称,可以使用calendar模块来帮助你。具体的代码如下:
```python
import datetime
import calendar
now = datetime.datetime.now()
month = now.month
month_name = calendar.month_name[month]
print("当前月份为:", month_name)
```
输出结果类似于:
```
当前月份为: May
```