星期二, 九月 04, 2007

python datetime.timedelta

>>> import time, datetime
>>> d = datetime.datetime(*time.localtime()[:6])
>>> print d
2007-09-04 10:10:00
>>> d + 15
Traceback (most recent call last):
File "", line 1, in
TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'int'
>>> d
datetime.datetime(2007, 9, 4, 10, 10)
>>> t = datetime.timedelta(days=15)
>>> d - t
datetime.datetime(2007, 8, 20, 10, 10)
>>> x = d - t
这样就可以得到表示 15 天前的那个时间对象。

没有评论: