Cron Next Execution API: The Complete Guide

Need to calculate when a cron job will run next? This guide covers everything you need to know about calculating cron execution times via API, including timezone handling, multiple occurrences, and implementation examples in multiple languages.

What is Cron Next Execution?

Cron next execution refers to calculating when a cron schedule will trigger next. Given a cron expression like 0 9 * * 1-5, you might need to know exactly when it will run next, which could be tomorrow at 9 AM or Monday at 9 AM depending on the current day.

This calculation considers the current time, timezone, and the cron expression's constraints to determine the precise next execution timestamp.

Why Calculate Next Execution?

There are several important reasons to calculate cron next execution times:

  • User Display: Show users when their scheduled job will run next
  • Testing: Verify that cron expressions produce expected schedules
  • Countdown Timers: Display time remaining until next execution
  • Resource Planning: Anticipate when resources will be needed
Pro Tip: Always specify a timezone when calculating next execution times to ensure consistent results across different environments.

Using the Cron Next API

TinyFn provides a simple endpoint to calculate cron next execution:

API Request
GET https://api.tinyfn.io/v1/cron/next?expression=0 9 * * 1-5
Headers: X-API-Key: your-api-key
Response
{
  "expression": "0 9 * * 1-5",
  "next": "2024-01-22T09:00:00Z",
  "next_formatted": "Monday, January 22, 2024 at 9:00 AM UTC",
  "seconds_until": 86400,
  "timezone": "UTC"
}

Parameters

Parameter Type Description
expression string The cron expression (required)
timezone string Timezone for calculation (default: UTC)
count integer Number of upcoming executions to return (default: 1, max: 10)

Code Examples

JavaScript / Node.js

const response = await fetch(
  'https://api.tinyfn.io/v1/cron/next?expression=0 9 * * 1-5&count=5',
  { headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(`Next run: ${data.next_formatted}`);
console.log(`Seconds until: ${data.seconds_until}`);

Python

import requests

response = requests.get(
    'https://api.tinyfn.io/v1/cron/next',
    params={'expression': '0 9 * * 1-5', 'timezone': 'America/New_York'},
    headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(f"Next run: {data['next_formatted']}")

cURL

curl "https://api.tinyfn.io/v1/cron/next?expression=0%209%20*%20*%201-5&timezone=UTC" \
  -H "X-API-Key: your-api-key"

Common Use Cases

  • Dashboard Displays: Show "Next run in 2 hours 15 minutes" on job dashboards
  • Email Notifications: Tell users when their scheduled reports will be sent
  • Schedule Preview: Let users preview when a new cron expression will trigger
  • Monitoring: Track if scheduled jobs are running on time
  • Calendar Integration: Add upcoming scheduled events to calendars

Best Practices

  1. Always specify timezone: Don't rely on defaults; explicitly set the timezone
  2. Cache results: Cache next execution times and refresh periodically
  3. Handle DST: Be aware of daylight saving time transitions
  4. Show relative time: Display "in 2 hours" alongside absolute timestamps

Try the Cron Next API

Get your free API key and start calculating cron schedules in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key