1 from datetime import timedelta, date, datetime 2 3 def get_day_of_day(n=7): 4 return date.today() - timedelta(days=n) 5 #today 6 date=get_day_of_day(0) 7 print date 8 9 #yesterday 10 date=get_day_of_day(1) 11 print date 12 13 #before 7 days 14 date_list=[] 15 for index in range(0, 7): 16 six_date = get_day_of_day(index) 17 str_six_date = datetime.strftime(six_date, '%Y%m%d') 18 date_list.append(str_six_date) 19 print date_list 20 21 #逆序 22 date_list=date_list[::-1] 23 print date_list 24 25 #指定格式时间串 26 date=datetime.now().strftime('%Y%m%d %H:%M:%S') 27 print date 28 29 #before 1 hour 时间戳 30 import time 31 start_time = (datetime.now() - timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S") 32 start = int(time.mktime(time.strptime(start_time, '%Y-%m-%d %H:%M:%S'))) 33 print start 34 35 #before 5 minutes 时间戳 36 start_time = (datetime.now() - timedelta(minutes=5)).strftime("%Y-%m-%d %H:%M:%S")