Datetime - 10 Hours
Consider:
now = datetime.datetime.now()
now
datetime.datetime(2009, 11, 6, 16, 6, 4开发者_JS百科2, 812098)
How would I create a new datetime object (past
) and minus n
values from the hours?
Use timedelta
in the datetime
module:
import datetime
now = datetime.datetime.now()
past = now - datetime.timedelta(hours=10)
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)
Use a timedelta object.
from datetime import datetime
back = datetime.now() - timedelta(hours=10)
精彩评论