如何用py输出当前时间
时间: 2023-04-06 18:03:41 浏览: 57
你可以使用Python中的datetime模块来输出当前时间。具体代码如下:
```python
import datetime
now = datetime.datetime.now()
print("当前时间为:", now)
```
输出结果类似于:
```
当前时间为: 2021-08-11 14:30:00.123456
```
其中,now()函数返回当前日期和时间的datetime对象,可以通过strftime()方法来格式化输出。
相关问题
py获取当前时间
可以使用 Python 自带的 datetime 模块来获取当前时间。以下是获取当前时间并以指定格式输出的示例代码:
```python
import datetime
# 获取当前时间
now = datetime.datetime.now()
# 格式化输出当前时间
print("当前时间为:", now.strftime("%Y-%m-%d %H:%M:%S"))
```
输出结果类似于:
```
当前时间为: 2022-05-12 15:30:45
```
py用turtle输出年分秒
可以通过以下代码使用turtle库输出年分秒:
```python
import turtle
import datetime
# 获取当前时间
now = datetime.datetime.now()
# 输出年份
turtle.write("Year: {}".format(now.year), align="center", font=("Arial", 16, "normal"))
turtle.penup()
turtle.forward(100)
turtle.pendown()
# 输出月份
turtle.write("Month: {}".format(now.month), align="center", font=("Arial", 16, "normal"))
turtle.penup()
turtle.forward(100)
turtle.pendown()
# 输出日期
turtle.write("Day: {}".format(now.day), align="center", font=("Arial", 16, "normal"))
turtle.penup()
turtle.backward(200)
turtle.right(90)
turtle.forward(50)
turtle.left(90)
turtle.pendown()
# 输出小时
turtle.write("Hour: {}".format(now.hour), align="center", font=("Arial", 16, "normal"))
turtle.penup()
turtle.forward(100)
turtle.pendown()
# 输出分钟
turtle.write("Minute: {}".format(now.minute), align="center", font=("Arial", 16, "normal"))
turtle.penup()
turtle.forward(100)
turtle.pendown()
# 输出秒钟
turtle.write("Second: {}".format(now.second), align="center", font=("Arial", 16, "normal"))
turtle.done()
```
这段代码会在窗口中输出当前时间的年份、月份、日期、小时、分钟和秒钟。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)