有没有比以下更好的方法以YYYY-MM-DD格式返回今天的日期?

str(datetime.datetime.today()).split()[0]

当前回答

从日期中获取日期是在python中

例如:19-12-2020(dd mm yyy)order_date我们需要19作为输出

order['day'] = order['Order_Date'].apply(lambda x: x.day)

其他回答

您可以使用datetime.date.today()将生成的datetime.date对象转换为字符串:

from datetime import date
today = str(date.today())
print(today)   # '2017-12-26'

我的代码有点复杂,但我经常使用它

strftime("%y_%m_%d", localtime(time.time()))

引用:'https://strftime.org/

你可以查看参考资料来制作任何你想要的东西YYYY-MM-DD只需将我的代码更改为:

strftime("%Y-%m-%d", localtime(time.time()))

从日期中获取日期是在python中

例如:19-12-2020(dd mm yyy)order_date我们需要19作为输出

order['day'] = order['Order_Date'].apply(lambda x: x.day)

很晚才回答,但您可以简单地使用:

import time
today = time.strftime("%Y-%m-%d")
# 2023-02-08

这是有效的:

from datetime import date
today =date.today()

本次产量:2020-08-29

其他:

this_year = date.today().year
this_month = date.today().month
this_day = date.today().day
print(today)
print(this_year)
print(this_month)
print(this_day)