get the date 5 weeks ago in python

Here's the code to get the date 5 weeks ago from the current date in Python using the datetime and timedelta modules:

main.py
from datetime import datetime, timedelta

# Get the date 5 weeks ago from today
five_weeks_ago = datetime.now() - timedelta(weeks=5)

# Print the result
print(five_weeks_ago.date())
182 chars
8 lines

In this code, we first import the necessary modules, datetime and timedelta. We then get the current date and subtract 5 weeks from it using timedelta. Finally, we print the resulting date using datetime.date().

gistlibby LogSnag