话不多说,有图有源码:
源码:
if content[searchopdate]=='今天': strbdate = date.today().strftime('%Y-%m-%d') stredate = date.today().strftime('%Y-%m-%d') elif content[searchopdate]=='本周': def get_current_week_dates(): today = datetime.today() first_day_of_week = today - relativedelta(days=today.weekday()) last_day_of_week = first_day_of_week + relativedelta(days=6) return first_day_of_week.date().strftime('%Y-%m-%d'), last_day_of_week.date().strftime('%Y-%m-%d') strbdate, stredate = get_current_week_dates() elif content[searchopdate] == '本月': this_month = date.today() next_month = date.today() + relativedelta(months=1) strbdate = (date(this_month.year, this_month.month, 1)).strftime('%Y-%m-%d') stredate = (date(next_month.year, next_month.month, 1) - relativedelta(days=1)).strftime('%Y-%m-%d') elif content[searchopdate] == '本季': def get_quarter_range(date): # 获取当前季度的第一天 first_day_of_quarter = date.replace(month=1 + ((date.month - 1) // 3) * 3, day=1) # 获取当前季度的最后一天 last_day_of_quarter = first_day_of_quarter + pd.offsets.QuarterEnd(0) return first_day_of_quarter.date().strftime('%Y-%m-%d'), last_day_of_quarter.date().strftime( '%Y-%m-%d') today = pd.to_datetime('today') strbdate, stredate = get_quarter_range(today) elif content[searchopdate] == '今年': today = date.today() strbdate = (date(today.year, 1, 1)).strftime('%Y-%m-%d') stredate = (date(today.year, 12, 31)).strftime('%Y-%m-%d') elif content[searchopdate] == '明天': today = datetime.now() newday = today + timedelta(days=1) strbdate = newday.strftime('%Y-%m-%d') stredate = newday.strftime('%Y-%m-%d') elif content[searchopdate]=='下周': def get_next_week_dates(): now = datetime.now() last_week_same_day = now + relativedelta(weeks=1) last_week_start = last_week_same_day - timedelta(days=last_week_same_day.weekday()) last_week_end = last_week_start + timedelta(days=6) return last_week_start, last_week_end next_week_start, next_week_end = get_next_week_dates() strbdate = next_week_start.strftime('%Y-%m-%d') stredate = next_week_end.strftime('%Y-%m-%d') elif content[searchopdate] == '下月': next_month = date.today() + relativedelta(months=1) next_last_month = date.today() + relativedelta(months=2) strbdate = (date(next_month.year, next_month.month, 1)).strftime('%Y-%m-%d') stredate = (date(next_last_month.year, next_last_month.month, 1) - relativedelta(days=1)).strftime( '%Y-%m-%d') elif content[searchopdate] == '下季': def get_next_quarter_range(date): # 得到下季度的第一天 first_day_of_previous_quarter = datetime(date.year, (date.month - 1) * 3 + 1, 1) - relativedelta(months=3) # 得到下季度的最后一天 last_day_of_previous_quarter = first_day_of_previous_quarter + relativedelta(months=3, days=-1) return first_day_of_previous_quarter.date().strftime( '%Y-%m-%d'), last_day_of_previous_quarter.date().strftime('%Y-%m-%d') current_date = datetime.now().date() strbdate, stredate = get_next_quarter_range(current_date) elif content[searchopdate] == '明年': today = date.today() + relativedelta(years=1) strbdate = (date(today.year, 1, 1)).strftime('%Y-%m-%d') stredate = (date(today.year, 12, 31)).strftime('%Y-%m-%d') elif content[searchopdate] == '昨天': today = datetime.now() newday = today - timedelta(days=1) strbdate = newday.strftime('%Y-%m-%d') stredate = newday.strftime('%Y-%m-%d') elif content[searchopdate]=='上周': def get_last_week_dates(): now = datetime.now() last_week_same_day = now - relativedelta(weeks=1) last_week_start = last_week_same_day - timedelta(days=last_week_same_day.weekday()) last_week_end = last_week_start + timedelta(days=6) return last_week_start, last_week_end last_week_start, last_week_end = get_last_week_dates() strbdate = last_week_start.strftime('%Y-%m-%d') stredate = last_week_end.strftime('%Y-%m-%d') elif content[searchopdate] == '上月': this_month = date.today() last_month = date.today() - relativedelta(months=1) strbdate = (date(last_month.year, last_month.month, 1)).strftime('%Y-%m-%d') stredate = (date(this_month.year, this_month.month, 1) - relativedelta(days=1)).strftime('%Y-%m-%d') elif content[searchopdate] == '上季': def get_previous_quarter_dates(date): # 计算指定日期所在季度的第一天 first_day_of_quarter = datetime(date.year, (date.month - 1) // 3 * 3 + 1, 1) # 计算上一季度的第一天 first_day_of_previous_quarter = first_day_of_quarter - relativedelta(months=3) # 上一季度的起始日期 start_date = first_day_of_previous_quarter.strftime('%Y-%m-%d') # 上一季度的结束日期 end_date = (first_day_of_quarter - timedelta(days=1)).strftime('%Y-%m-%d') return start_date, end_date today = datetime.today() strbdate,stredate = get_previous_quarter_dates(today) elif content[searchopdate] == '去年': today = date.today() - relativedelta(years=1) strbdate = (date(today.year, 1, 1)).strftime('%Y-%m-%d') stredate = (date(today.year, 12, 31)).strftime('%Y-%m-%d')
呕心之作,干货满满,相信对你绝对有价值
对你有用,请来个点赞+关注+转发吧!
还没有评论,来说两句吧...