cron-converter
Cron string parser for node and the browser
Try the online demo and check the source code for the integration.
Install
yarn add cron-converter
or
npm install cron-converter --save
Compatibility
Version 2.1.0
introduces support for the non standard L
character which represents the last day of the month. This feature is on by default and can be turned off by setting the enableLastDayOfMonth
option to false
.
Versions 2.x.x
are not backwards compatible with versions 1.x.x
.
Import
import { stringToArray, arrayToString, getSchedule, getUnits } from "cron-converter";
Usage
Convert a string to an array
const arr = stringToArray("*/10 9-17 1 * *");
console.log(arr);
Convert an array to a string
const str = arrayToString([[0], [1], [1], [5], [0, 2, 4, 6]]);
console.log(str);
Options
outputMonthNames
Default: false
const arr = [[1], [1], [1], [1, 2, 3], [1, 2, 3]];
const str = arrayToString(arr, { outputMonthNames: true });
console.log(str);
outputWeekdayNames
Default: false
const arr = [[1], [1], [1], [1, 2, 3], [1, 2, 3]];
const str = arrayToString(arr, { outputWeekdayNames: true });
console.log(str);
outputHashes
Default: false
const arr = [[1], [1], [1], [1, 6, 11], [0, 1, 2, 3, 4, 5, 6]];
console.log(arrayToString(arr, { outputHashes: true }));
enableLastDayOfMonth
Default: true
const arr = [[1], [1], [1], [-1], [1]];
console.log(arrayToString(arr, { enableLastDayOfMonth: true }));
const str = '1 1 1 L 1';
console.log(stringToArray(str, { enableLastDayOfMonth: true }));
Get the schedule execution times
const arr = stringToArray("*/5 * * * *");
let schedule = getSchedule(arr);
let reference = new Date(2013, 2, 8, 9, 32);
schedule = getSchedule(arr, reference, "Europe/London");
console.log(schedule.next().format());
console.log(schedule.next().format());
schedule.reset();
console.log(schedule.prev().format());
console.log(schedule.prev().format());
Get the units configuration
This is useful if you are creating a user interface. See units.ts.
const units = getUnits();
Test and build
git clone https://github.com/roccivic/cron-converter
cd cron-converter
yarn
yarn build
yarn test
yarn coverage