1.获取字符串型当前日期 2016-10-09格式
import datetimetoday = datetime.date.today() #datetime.date类型当前日期str_today = str(today) #字符串型当前日期
2.获取datetime.datetime类型当前日期
from datetime import datetime, date, timedelta def getDatetimeToday(self): t = date.today() #date类型 dt = datetime.strptime(str(t),'%Y-%m-%d') #date转str再转datetime return dt
3.获取datetime.datetime类型前一天日期
from datetime import datetime, date, timedelta def getDatetimeYesterday(self): today = self.getDatetimeToday() #datetime类型当前日期 yesterday = today + timedelta(days = -1) #减去一天 return yesterday