python 获取现在时间
时间: 2023-07-01 20:18:03 浏览: 63
可以使用 Python 的 `datetime` 模块来获取当前时间。下面是一个示例代码:
```python
import datetime
# 获取当前时间
now = datetime.datetime.now()
# 输出当前时间
print("当前时间为:", now)
```
这段代码中,我们使用 `datetime.datetime.now()` 函数来获取当前时间,并将其保存在 `now` 变量中。然后使用 `print()` 函数来输出当前时间。输出的结果类似于:
```
当前时间为: 2021-08-19 15:23:45.678901
```
其中,日期和时间之间的空格可以使用 `now.strftime('%Y-%m-%d %H:%M:%S')` 来格式化输出。
相关问题
python获取现在时间 datetime格式
可以使用Python的datetime模块来获取现在的时间,代码如下:
```python
from datetime import datetime
now = datetime.now()
print(now)
```
这将输出当前的时间,包括年、月、日、时、分、秒和微秒。如果你只需要日期或时间的一部分,可以使用`now.date()`或`now.time()`方法。
python获取现在日期
可以使用Python中的datetime模块来获取当前的日期和时间,示例如下:
```python
import datetime
now = datetime.datetime.now()
print("当前日期和时间:", now)
```
执行结果类似于:
```
当前日期和时间: 2021-12-06 16:38:14.469264
```
其中,datetime.now()方法会返回一个datetime对象,表示当前的日期和时间,可以进一步使用该对象获取具体的年、月、日、时、分、秒等信息。