#!/usr/bin/env python import datetime # 获取过去几天的日期 def get_last_days(delta): days = [] now = datetime.datetime.now() for i in range(1, delta): last_date = now + datetime.timedelta(days=-i) last_date_str = last_date.strftime('%Y-%m-%d') # 格式化输出 days.append(last_date_str) days.sort() return days class DateNext(object): def get_next_week_date(self, weekday): # 获取下周几的日期 # weekday取值如下: # (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7) today = datetime.date.today() onday = datetime.timedelta(days=1) # m1 = calendar.MONDAY while today.weekday() != weekday: today += onday # logging.info(datetime.date(today.year, today.month, today.day)) nextWeekDay = today.strftime('%Y-%m-%d') return nextWeekDay def get_next_month_date(self, day_of_the_month): # 获取下个月几号的日期 today_date = datetime.datetime.today() today = today_date.day if today == day_of_the_month: return today_date.strftime('%Y-%m-%d') if today < day_of_the_month: return datetime.date(today_date.year, today_date.month, day_of_the_month).strftime('%Y-%m-%d') if today > day_of_the_month: if today_date.month == 12: return datetime.date(today_date.year+1, 1, day_of_the_month).strftime('%Y-%m-%d') return datetime.date(today_date.year, today_date.month+1, day_of_the_month).strftime('%Y-%m-%d')