#!/usr/bin/env python import datetime from datetime import datetime, timedelta # 获取过去几天的日期 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 def time_diff(t1): # 将字符串转换为 datetime 对象 t1 = datetime.strptime(t1, "%Y-%m-%d %H:%M:%S") # 计算当前时间与 t1 的差值 diff = datetime.now() - t1 # 计算天数、小时数和分钟数 days = diff.days hours, remainder = divmod(diff.seconds, 3600) minutes, seconds = divmod(remainder, 60) # 返回格式化的时间差 return f"{days}:{hours}:{minutes}" 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')