Python 获取当前时间
时间: 2024-08-16 08:03:03 浏览: 69
在Python中,你可以使用内置的`datetime`模块来获取当前的时间。下面是一个简单的例子:
```python
from datetime import datetime
# 获取当前日期和时间
current_time = datetime.now()
print("当前时间为:", current_time)
# 只打印当前时间部分(默认格式)
print("当前时间为:", current_time.strftime("%Y-%m-%d %H:%M:%S"))
相关问题
python 获取当前时间
可以使用 Python 内置的 datetime 模块来获取当前时间。以下是示例代码:
```python
import datetime
now = datetime.datetime.now()
print("当前时间:", now)
```
输出结果类似于:
```
当前时间: 2021-10-14 15:50:23.123456
```
你也可以使用 strftime() 方法来格式化时间输出。例如:
```python
import datetime
now = datetime.datetime.now()
print("当前时间:", now.strftime("%Y-%m-%d %H:%M:%S"))
```
输出结果类似于:
```
当前时间: 2021-10-14 15:50:23
```
python获取当前时间
可以使用Python内置的datetime模块获取当前时间,示例代码如下:
```
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%Y-%m-%d %H:%M:%S")
print("当前时间为:", current_time)
```
运行结果如下:
```
当前时间为: 2021-11-04 14:48:00
```
阅读全文
相关推荐









