Python Convert a Cron Expression to the Total Number of Runs/Executions in One Day

Recently, I was working on a project and required to convert cron expression to the total number of executions in one day. There are some libraries in Python that help you to convert cron expressions into human-readable ( texts. However, in my case, I needed to get the total execution.

I found a library ( that can give you the exact execution time. I used it to extract the total number of execution in a day.

import croniter
import datetime

def getTotalNumberOfExecution(cron_str):
        cur_date =
        #put the current day (start from 00:00AM) to get its all executions
        now = datetime.datetime(year=cur_date.year,month=cur_date.month,,hour=0,minute=0,second=0)
        cron = croniter.croniter(cron_str, now)
        nextdate = cron.get_next(datetime.datetime)
        count = 0
        while <= #loop over the iterations until it goes to the next day
            nextdate = cron.get_next(datetime.datetime)
            count += 1
       return count
       if cron_str == "@once": #here you can handle cron strings starts with @
            return 1
       return 0

# Main #
cron_str = "0 * * * *"

I hope the above code would help to complete this task. The code is self-explanatory, but if you have any question you can ask me in the comments.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s