python获取当前月份
时间: 2023-06-30 19:11:27 浏览: 146
您可以使用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
```
python获取当前时间
在Python中,可以使用内置的`datetime`模块来获取当前的时间。以下是获取当前时间的代码示例:
```python
import datetime
# 获取当前日期和时间
now = datetime.datetime.now()
# 获取当前年份
year = now.year
# 获取当前月份
month = now.month
# 获取当前天数
day = now.day
# 获取当前小时数
hour = now.hour
# 获取当前分钟数
minute = now.minute
# 获取当前秒数
second = now.second
# 打印当前日期和时间
print("当前时间为:", now)
# 打印当前年份
print("当前年份为:", year)
# 打印当前月份
print("当前月份为:", month)
# 打印当前天数
print("当前天数为:", day)
# 打印当前小时数
print("当前小时数为:", hour)
# 打印当前分钟数
print("当前分钟数为:", minute)
# 打印当前秒数
print("当前秒数为:", second)
```
输出结果类似于:
```
当前时间为: 2021-08-09 13:45:30.123456
当前年份为: 2021
当前月份为: 8
当前天数为: 9
当前小时数为: 13
当前分钟数为: 45
当前秒数为: 30
```
注意:以上示例代码中的`123456`是微秒数,如果不需要可以省略。