You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现获取每月及季度第三个周五日期列表的函数?

实现每月/季度第三个周五的日期生成函数

一、每月第三个周五函数 thirdFridays

核心逻辑:遍历时间区间覆盖的所有月份,计算每个月的第三个周五,再筛选出落在区间内的日期。

步骤拆解:

  1. 解析输入的起始、结束日期,确定需要处理的年月范围
  2. 对每个月份,从当月1号出发,计算到第一个周五的偏移天数(周五对应weekday=4,周一为0,周日为6)
  3. 第一个周五加上14天(两周)即为当月第三个周五
  4. 检查该日期是否在输入区间内,符合条件则加入结果列表

示例Python实现:

from datetime import datetime, timedelta

def thirdFridays(start_date_str, end_date_str):
    # 解析日期字符串(格式:YYYY.MM.DD)
    start = datetime.strptime(start_date_str, "%Y.%m.%d")
    end = datetime.strptime(end_date_str, "%Y.%m.%d")
    result = []
    
    current_year, current_month = start.year, start.month
    while (current_year, current_month) <= (end.year, end.month):
        # 获取当月1号
        first_day = datetime(current_year, current_month, 1)
        # 计算第一个周五的偏移天数
        days_to_first_friday = (4 - first_day.weekday()) % 7
        first_friday = first_day + timedelta(days=days_to_first_friday)
        # 推导第三个周五
        third_friday = first_friday + timedelta(days=14)
        
        if start <= third_friday <= end:
            result.append(third_friday.strftime("%Y.%m.%d"))
        
        # 切换到下一个月
        if current_month == 12:
            current_year += 1
            current_month = 1
        else:
            current_month += 1
    
    return result

二、季度第三个周五函数 quarterlyThirdFridays

核心逻辑:仅处理每个季度的首月(1、4、7、10月),计算该月的第三个周五,再筛选落在区间内的日期。

步骤拆解:

  1. 确定区间覆盖的所有季度首月(1、4、7、10月中,年月在起始和结束日期之间的)
  2. 对每个季度首月,复用每月第三个周五的计算逻辑
  3. 检查日期是否在区间内,符合条件则加入结果列表

示例Python实现:

def quarterlyThirdFridays(start_date_str, end_date_str):
    start = datetime.strptime(start_date_str, "%Y.%m.%d")
    end = datetime.strptime(end_date_str, "%Y.%m.%d")
    result = []
    # 季度首月集合
    quarter_months = [1, 4, 7, 10]
    
    current_year = start.year
    # 定位起始年份需要处理的首个季度月
    start_month = next((m for m in quarter_months if m >= start.month), quarter_months[0])
    if start_month == quarter_months[0] and current_year > start.year:
        current_year += 1
    
    while True:
        for month in quarter_months:
            # 跳过早于起始日期的季度月
            if current_year < start.year or (current_year == start.year and month < start.month):
                continue
            # 超出结束日期范围则终止循环
            if current_year > end.year or (current_year == end.year and month > end.month):
                break
            
            first_day = datetime(current_year, month, 1)
            days_to_first_friday = (4 - first_day.weekday()) % 7
            first_friday = first_day + timedelta(days=days_to_first_friday)
            third_friday = first_friday + timedelta(days=14)
            
            if start <= third_friday <= end:
                result.append(third_friday.strftime("%Y.%m.%d"))
        else:
            current_year += 1
            continue
        break
    
    return result

测试示例:

# 测试每月第三个周五
print(thirdFridays("2019.01.01", "2019.12.31"))
# 输出:['2019.01.18', '2019.02.15', '2019.03.15', '2019.04.19', '2019.05.17', '2019.06.21', '2019.07.19', '2019.08.16', '2019.09.20', '2019.10.18', '2019.11.15', '2019.12.20']

# 测试季度第三个周五
print(quarterlyThirdFridays("2019.01.01", "2019.12.31"))
# 输出:['2019.01.18', '2019.04.19', '2019.07.19', '2019.10.18']

内容的提问来源于stack exchange,提问作者BlackPearl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 09:02:54