有没有比以下更好的方法以 YYYY-MM-DD
格式返回今天的日期?
str(datetime.datetime.today()).split()[0]
最佳答案
使用 strftime
:
>>> from datetime import datetime
>>> datetime.today().strftime('%Y-%m-%d')
'2021-01-26'
还要在末尾添加一个零填充的 Hour:Minute:Second
:
>>> datetime.today().strftime('%Y-%m-%d %H:%M:%S')
'2021-01-26 16:50:03'
https://stackoverflow.com/questions/32490629/