python3 按月执行定时任务,保证每月都有执行任务(2月,30天,31天做了处理)

def mouthRange(beginDate, endDate, shu_day, schedule_time):
    dates = []
    new_mouths = []
    dt = datetime.datetime.strptime(beginDate, "%Y-%m-%d %H:%M")
    date = beginDate[:]
    while date <= endDate:
        dates.append(date)
        dt = dt + datetime.timedelta(1)
        date = dt.strftime("%Y-%m-%d %H:%M")
    da_mouth = []
    for i in dates:
        if i[:7] not in da_mouth:
            da_mouth.append(i[:7])
    # schedule_time = "17:20"
    # shu_day = 1
    # current_time = datetime.datetime.now()
    # current_time = current_time.strftime("%Y-%m-%d %H:%M")
    # current_time = datetime.datetime.strptime(current_time, "%Y-%m-%d %H:%M")
    # big_time = datetime.datetime.strptime(beginDate, "%Y-%m-%d %H:%M")
    # print(current_time)
    # print(big_time)
    # if current_time > dt:
    #     print("12jpkp")
    #     da_mouth = da_mouth[1:]
    first_mouth = int(beginDate[8:10])
    print(beginDate[8:10])
    if first_mouth <= shu_day:
        new_mouths.append(da_mouth[0] + "-" +str(shu_day) + " "+ schedule_time)
    for i in da_mouth[1:]:
        days = calendar.monthrange(int(i[:4]), int(i[5:7]))[1]
        if shu_day < 29:
            new_mouths.append(i+"-"+str(shu_day).zfill(2) +" "+schedule_time)
        if shu_day == 29:
            if days >= 29:
                new_mouths.append(i+"-"+str(shu_day) +" "+schedule_time)
            else:
                new_mouths.append(i+"-"+"28" +" "+schedule_time)
        elif shu_day == 30:
            if days == 28:
                new_mouths.append(i+"-"+str(days) +" "+schedule_time)
            elif days== 29 :
                new_mouths.append(i+"-"+"29" +" "+schedule_time)
            elif days >= 30:
                new_mouths.append(i+"-"+"30" +" "+schedule_time)
        elif shu_day == 31:
            if days == 28:
                new_mouths.append(i+"-"+str(days) +" "+schedule_time)
            elif days== 29 :
                new_mouths.append(i+"-"+"29" +" "+schedule_time)
            elif days == 30:
                new_mouths.append(i+"-"+"30" +" "+schedule_time)
            elif days == 31:
                new_mouths.append(i+"-"+"31" +" "+schedule_time)
        else:
            pass
    last_time = datetime.datetime.strptime(new_mouths[-1], "%Y-%m-%d %H:%M")
    endDate_time = datetime.datetime.strptime(endDate, "%Y-%m-%d %H:%M")
    if last_time > endDate_time:
        new_mouths =  new_mouths[:-1]
    return new_mouths
print(mouthRange('2018-08-15 11:00', '2018-09-15 12:30', 15, "12:00"))

输出结果:


 

解释说明:

beginDate 为 定时任务 开始执行日期

endDate   为定时任务  结束执行日期

shu_day  为 每月几号 执行定时任务

schedule_time  为定时任务中每次执行的时间

此代码也可以找到每月的最后一天,也就是每月最后一天执行,包含特殊月份的处理

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值