import datetime
#现在
nowTime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
#过去一小时时间
pastTime = (datetime.datetime.now()-datetime.timedelta(hours=1)).strftime('%Y-%m-%d %H:%M:%S')
#明天
tomorrowTime = (datetime.datetime.now()+datetime.timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S')
#后天
afterTomorrowTime = (datetime.datetime.now()+datetime.timedelta(days=2)).strftime('%Y-%m-%d %H:%M:%S')
Python 获取昨天日期
import datetime
def getYesterday():
today=datetime.date.today()
oneday=datetime.timedelta(days=1)
yesterday=today-oneday
return yesterday
计算日期之间相差天数
import datetime
#strptime() 函数根据指定的格式把一个时间字符串解析为时间元组
date1 = datetime.datetime.strptime('2018-12-01 10:40:20',"%Y-%m-%d %H:%M:%S").date() #获取日期
#time1 = datetime.datetime.strptime('2018-12-01 10:40:20',"%Y-%m-%d %H:%M:%S").time() #获取时间
today = datetime.date.today()
print((today-date1).days)