Getting the date of 7 days ago from current date in python
PythonDatetimePython Problem Overview
I'm trying to get the date that was 7 days ago starting from current date in python. Can anyone help me?
Python Solutions
Solution 1 - Python
import datetime as DT
today = DT.date.today()
week_ago = today - DT.timedelta(days=7)
Solution 2 - Python
>>> import datetime
>>> datetime.datetime.now() - datetime.timedelta(days=7)
datetime.datetime(2013, 12, 6, 10, 29, 37, 596779)
If you really just want the date, you can call the date method:
>>> (datetime.datetime.now() - datetime.timedelta(days=7)).date()
datetime.date(2013, 12, 6)
Or, work with dates to begin with as suggested by unutbu.