Answers:
>>> from datetime import date, timedelta
>>> yesterday = date.today() - timedelta(days=1)
>>> yesterday.strftime('%m%d%y')
'110909'
from datetime import datetime, timedelta
yesterday = datetime.now() - timedelta(days=1)
yesterday.strftime('%m%d%y')
所有答案都是正确的,但是我想提到时间增量接受否定论点。
>>> from datetime import date, timedelta
>>> yesterday = date.today() + timedelta(days=-1)
>>> print(yesterday.strftime('%m%d%y')) #for python2 remove parentheses
我可以将其设置为更具国际性,并根据国际标准设置日期格式,而不是在美国常见的怪异的月-日-年格式中吗?
from datetime import datetime, timedelta
yesterday = datetime.now() - timedelta(days=1)
yesterday.strftime('%Y-%m-%d')
扩展克里斯给出的答案
如果您想以特定格式将日期存储在变量中,就我所知,这是最短,最有效的方法
>>> from datetime import date, timedelta
>>> yesterday = (date.today() - timedelta(days=1)).strftime('%m%d%y')
>>> yesterday
'020817'
如果您希望将其作为整数(可能会有用)
>>> yesterday = int((date.today() - timedelta(days=1)).strftime('%m%d%y'))
>>> yesterday
20817
print((pd.to_datetime('Today') - pd.Timedelta('1 days')).strftime('%m%d%y'))