In Python, how to I create a datetime with X hours?
H开发者_如何学运维ow do I create the datetime that is 24 hours before NOW()? 48 before NOW()? X hours or days before now??
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)
>>> from datetime import datetime, timedelta
>>> datetime.now() - timedelta(hours=24)
datetime.datetime(2010, 5, 18, 14, 5, 56, 128000)
精彩评论