Answers:
导入datetime,然后导入神奇的timedelta内容:
In [63]: datetime.datetime.now()
Out[63]: datetime.datetime(2010, 12, 27, 14, 39, 19, 700401)
In [64]: datetime.datetime.now() - datetime.timedelta(minutes=15)
Out[64]: datetime.datetime(2010, 12, 27, 14, 24, 21, 684435)
now()
以避免出现有关减去偏移天真和偏移datetime.datetime.now(datetime.timezone.utc)
如果您想查看更多示例,那么在几分钟,几年和几小时内,我提供了两种方法:
import datetime
print(datetime.datetime.now())
print(datetime.datetime.now() - datetime.timedelta(minutes = 15))
print(datetime.datetime.now() + datetime.timedelta(minutes = -15))
print(datetime.timedelta(hours = 5))
print(datetime.datetime.now() + datetime.timedelta(days = 3))
print(datetime.datetime.now() + datetime.timedelta(days = -9))
print(datetime.datetime.now() - datetime.timedelta(days = 9))
我得到以下结果:
2016-06-03 16:04:03.706615
2016-06-03 15:49:03.706622
2016-06-03 15:49:03.706642
5:00:00
2016-06-06 16:04:03.706665
2016-05-25 16:04:03.706676
2016-05-25 16:04:03.706687
2016-06-03
16:04:03.706716
除了timedelta
对象
http://docs.python.org/library/datetime.html外,还使用DateTime
datetime.datetime.now()-datetime.timedelta(minutes=15)