<分区>
我有下面的代码,它从今天开始算出 6 个月前的日期。
import datetime
from dateutil.relativedelta import relativedelta
from datetime import date
six_months = date.today() + relativedelta(months=-6)
但是我希望 six_months 的格式为“%Y%m%d”。
感谢您的帮助。
<分区>
我有下面的代码,它从今天开始算出 6 个月前的日期。
import datetime
from dateutil.relativedelta import relativedelta
from datetime import date
six_months = date.today() + relativedelta(months=-6)
但是我希望 six_months 的格式为“%Y%m%d”。
感谢您的帮助。
最佳答案
您正在寻找 strftime
。
six_months.strftime('%Y%m%d')
这应该可以为您完成工作。
关于python - 如何在 Python 中将日期更改为 YYYYMMDD?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42298564/
相关文章:
python - raise TypeError('在 include() 的情况下, View 必须是可调用的或列表/元组
python - 类型错误 : object of type 'filter' has no len()
python - Pandas 数据透视表 : Percentage of row calculation for non-numeric values
sql - 如何使用 PostgreSQL 确定上个月的最后一天?
python - 在不更改 dtype 的情况下将日期时间转换为另一种格式
sql - 事务处理 SQL : Detect Date boundaries across multiple rows