How do I create the datetime that is 24 hours before NOW()? 48 before NOW()? X hours or days before now??
+1
A:
>>> from datetime import datetime, timedelta
>>> datetime.now() - timedelta(hours=24)
datetime.datetime(2010, 5, 18, 14, 5, 56, 128000)
Adam Bernier
2010-05-19 21:06:52
+3
A:
from datetime import datetime, timedelta
def hours_ago(hours):
return datetime.today() - timedelta(hours=hours)
def days_ago(days):
return datetime.today() - timedelta(days=days)
Tamás
2010-05-19 21:08:44