get the date 5 days ago in python

Here's one way to get the date 5 days ago using datetime and timedelta:

main.py
from datetime import datetime, timedelta

today = datetime.now().date()
five_days_ago = today - timedelta(days=5)

print(five_days_ago)
136 chars
7 lines

Alternatively, you could use dateutil to calculate a relative date:

main.py
from datetime import datetime
from dateutil.relativedelta import relativedelta

five_days_ago = datetime.now().date() - relativedelta(days=5)

print(five_days_ago)
164 chars
7 lines

gistlibby LogSnag