views:

122

answers:

2

Consider:

now = datetime.datetime.now()
now
datetime.datetime(2009, 11, 6, 16, 6, 42, 812098)

How would I create a new datetime object (past) and minus n values from the hours?

+12  A: 
from datetime import datetime, timedelta
now = datetime.now()
past = now - timedelta(hours=10)
Eric Palakovich Carr
+7  A: 

Use a timedelta object.

>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2009, 11, 6, 16, 35, 50, 593000)
>>> ten_hours = datetime.timedelta(hours=10)
>>> now + ten_hours
datetime.datetime(2009, 11, 7, 2, 35, 50, 593000)
>>> now - ten_hours
datetime.datetime(2009, 11, 6, 6, 35, 50, 593000)
Rod Hyde