Calculates the date of n days ago from today.
- Use
datetime.date.today()to get the current day. - Use
datetime.timedeltato subtractndays from today’s date.
代码实现
from datetime import timedelta, date
def days_ago(n):
return date.today() - timedelta(n)
使用样例
days_ago(5) # date(2020, 10, 23)