- 从datetime模块导入date函数
接收了三个参数,年,月,日,它将返回:年-月-日。
from datetime import date
date1 = date(2016, 1, 1)
date2 = date(2016, 7, 20)
print(date1, date2)
#2016-01-01 2016-07-20
- timedelta()函数
我先用date()函数输入三个参数(2016,8,5),today的值便是2016-8-5,那如果我要第二天的日期,也就是2016-8-6,即5+1,这时的+1便是+timedelta(days=1),里面参数days决定每一次加多少。
from datetime import date, timedelta
today = date(2016, 8, 5) #2016-8-5
days = timedelta(days=1) #1 day, 0:00:00
tomorrow = today + days
yesterday = today - days
print(tomorrow) # 2016-08-06
print(yesterday) # 2016-08-04