Skip to main content
Glama
cron.ts1.33 kB
import { CronExpressionParser } from 'cron-parser'; import cronValidate from 'cron-validate'; export function calculateNextRun(cronExpression: string, timezone: string, from?: Date): Date { // calculates the next run using the cron expression and timezone and returns the date in utc const currentDate = from || new Date(Date.now()); // Use provided date or current UTC time const parsed = CronExpressionParser.parse(cronExpression, { currentDate, tz: timezone, }); // cron-parser returns dates in the specified timezone, so we need to convert to UTC const nextRunInTimezone = parsed.next().toDate(); // Convert to UTC by using the ISO string representation return new Date(nextRunInTimezone.toISOString()); } export function validateCronExpression(cronExpression: string): boolean { try { const result = cronValidate(cronExpression, { preset: 'default', override: { useSeconds: false, useYears: false, useAliases: false, allowStepping: true, useLastDayOfMonth: false, useLastDayOfWeek: false, useNearestWeekday: false, }, }); return result.isValid(); } catch { return false; } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/superglue-ai/superglue'

If you have feedback or need assistance with the MCP directory API, please join our Discord server