python - 在 Python 中以 YYYY-MM-DD 获取今天的日期?

有没有比以下更好的方法以 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/

相关文章:

python - 如何检查变量是否是类?

linux - 使用sudo时如何保留环境变量

python - Python 是强类型的吗?

python - 在 Python 中获取临时目录的跨平台方法

linux - 如何递归查找并列出具有子目录和时间的目录中的最新修改文件

linux - 更改 crontab 文件后重新启动 cron?

linux - 如何使用自定义分隔符将多行文件名合并为一个?

linux - 在shell中获取程序执行时间

python - 如何逐行读取大型文本文件,而不将它们加载到内存中?

python - Django:显示选择值