Python简单日期算术
示例
日期不是孤立存在的。通常,您需要查找日期之间的时间量或确定明天是几号。这可以使用timedelta对象来完成
import datetime today = datetime.date.today() print('Today:', today) yesterday = today - datetime.timedelta(days=1) print('Yesterday:', yesterday) tomorrow = today + datetime.timedelta(days=1) print('Tomorrow:', tomorrow) print('Time between tomorrow and yesterday:', tomorrow - yesterday)
这将产生类似于以下结果:
Today: 2016-04-15 Yesterday: 2016-04-14 Tomorrow: 2016-04-16 Difference between tomorrow and yesterday: 2 days, 0:00:00