dbader / schedule

Python job scheduling for humans.
https://schedule.readthedocs.io/
MIT License
11.71k stars 960 forks source link

Run job on specific days of each month #549

Closed Rmfjd1978 closed 1 year ago

Rmfjd1978 commented 1 year ago

Is it possible to create a shedule to run on specific days of the month? What i want to do is to run a function on the last 10 days of each month

SijmenHuizenga commented 1 year ago

Yes, it is possible to create a schedule to run on specific days of the month using the schedule library. Below is an (untested) example that runs on the last 10 days of the month:

import schedule
import time
from datetime import datetime, timedelta

def job():
    if datetime.now().day not in get_last_10_days_of_month():
        # Wrong day
        return
    print("Job running...")

def get_last_10_days_of_month():
    last_day = datetime.now().replace(day=28).date().replace(day=1)
    last_10_days = [(last_day - timedelta(days=i)).day for i in range(1, 11)]
    return last_10_days

# Schedule the job to run every day at a specific time
schedule.every().day.at("12:00").do(job)

while True:
    schedule.run_pending()
    time.sleep(60)

Happy scheduling!