python获取年月日时分秒
时间: 2024-01-11 13:20:25 浏览: 82
asp提取年月日时分秒
4星 · 用户满意度95%
以下是两种Python获取当前时间的年月日时分秒的例子:
1. 使用time模块获取当前时间并格式化输出年月日时分秒:
```python
import time
now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print(now_time) # 输出:2022-08-09 16:43:35
```
2. 使用datetime模块获取当前时间的年月日时分秒:
```python
import datetime
today = datetime.datetime.today()
currentYear = today.year
currentMonth = today.month
currentDay = today.day
currentHour = today.hour
currentMinute = today.minute
currentSecond = today.second
print(currentYear, currentMonth, currentDay, currentHour, currentMinute, currentSecond) # 输出:2022 8 9 16 45 22
```
阅读全文