帮我写个python获取当前日期的代码,格式如下: 2023-06-09
时间: 2024-03-22 20:39:54 浏览: 17
当然可以!以下是获取当前日期并将其格式化为"2023-06-09"的Python代码:
```python
import datetime
now = datetime.datetime.now()
formatted_date = now.strftime("%Y-%m-%d")
print(formatted_date)
```
代码中,我们使用了Python内置的datetime模块,通过调用now()方法获取当前日期和时间,然后使用strftime()方法将其格式化为"2023-06-09"的形式。最后使用print()函数打印出来。
相关问题
写一个获取当前时间的python代码,要求输出格式位YYYY-MM-DD
可以使用Python标准库中的`datetime`模块获取当前时间,并使用`strftime`方法将日期时间格式化为指定的字符串格式,如下所示:
```python
from datetime import datetime
# 获取当前时间
now = datetime.now()
# 将时间格式化为指定的字符串格式
formatted_date = now.strftime('%Y-%m-%d')
# 输出格式化后的日期字符串
print(formatted_date)
```
在上面的示例中,`datetime.now()`方法获取当前时间,然后使用`strftime('%Y-%m-%d')`方法将时间格式化为`YYYY-MM-DD`的字符串格式,最后通过`print()`函数输出格式化后的日期字符串。
用python写一个获取当前日期的代码
可以使用Python内置的datetime模块来获取当前日期。下面是一个示例代码:
```python
import datetime
today = datetime.date.today()
print("今天的日期是:", today)
```
输出结果类似于:
```
今天的日期是: 2022-01-01
```
如果你想要获得更加详细的时间信息,可以使用datetime模块中的datetime类,示例如下:
```python
import datetime
now = datetime.datetime.now()
print("当前时间是:", now)
```
输出结果类似于:
```
当前时间是: 2022-01-01 12:00:00.000000
```