# cron-converter

> Cron string converter

Latest version **2.1.0** (published 2024-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install cron-converter
pnpm add cron-converter
yarn add cron-converter
bun add cron-converter
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2024-09-28 |
| First published | 2015-12-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 80.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 60 |
| Author | Rouslan Placella |
| Maintainers | roccivic |
| Keywords | cron, cronjob, crontab, schedule, parser |

## Links

- npm: https://www.npmjs.com/package/cron-converter
- Repository: https://github.com/roccivic/cron-converter
- Homepage: https://github.com/roccivic/cron-converter#readme
- Issues: https://github.com/roccivic/cron-converter/issues
- npm.io page: https://npm.io/package/cron-converter

## Dependencies (1)

- [luxon](https://npm.io/package/luxon.md) ^3.1.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2024-09-28
- 2.0.1 — 2023-08-03
- 2.0.0 — 2022-11-15
- 1.0.2 — 2022-07-20
- 1.0.1 — 2021-05-02
- 1.0.0 — 2019-09-29
- 0.0.12 — 2017-10-13
- 0.0.11 — 2016-01-27
- 0.0.10 — 2016-01-15
- 0.0.9 — 2016-01-11
- 0.0.8 — 2015-12-27
- 0.0.7 — 2015-12-26
- 0.0.6 — 2015-12-24
- 0.0.5 — 2015-12-19
- 0.0.4 — 2015-12-19
- … 3 more at https://npm.io/package/cron-converter/versions

## README

# cron-converter

Cron string parser for node and the browser

[![npm version](https://badge.fury.io/js/cron-converter.svg)](https://badge.fury.io/js/cron-converter)
[![Build status](https://github.com/roccivic/cron-converter/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/roccivic/cron-converter/actions/workflows/build.yml)
[![Coverage Status](https://coveralls.io/repos/roccivic/cron-converter/badge.svg?branch=master&service=github)](https://coveralls.io/github/roccivic/cron-converter?branch=master)

Try the [online demo](https://cron-converter-demo.netlify.app/) and check the [source code](https://github.com/roccivic/cron-converter-demo) for the integration.

# Install

```bash
yarn add cron-converter
```

or

```bash
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`.

| | `2.x.x`  | `1.x.x` |
| ---- | ------------- | ------------- |
| API | Functional | Object-oriented |
| Loader | ESM and CommonJS | CommonJS only |
| Type definitions | Bundled | Install [`@types/cron-converter`](https://www.npmjs.com/package/@types/cron-converter) |
| Date/time | [`Luxon`](https://moment.github.io/luxon/) | [`Moment.js`](https://momentjs.com/) |
| Tree-shaking | ✅ | ❌ |

# Import

```ts
import { stringToArray, arrayToString, getSchedule, getUnits } from "cron-converter";
```

# Usage

## Convert a string to an array

```ts
// Every 10 mins between 9am and 5pm on the 1st of every month
const arr = stringToArray("*/10 9-17 1 * *");

// Prints:
// [
//   [ 0, 10, 20, 30, 40, 50 ],
//   [ 9, 10, 11, 12, 13, 14, 15, 16, 17 ],
//   [ 1 ],
//   [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ],
//   [ 0, 1, 2, 3, 4, 5, 6 ]
// ]
console.log(arr);
```

## Convert an array to a string

```ts
const str = arrayToString([[0], [1], [1], [5], [0, 2, 4, 6]]);

// Prints: '0 1 1 5 */2'
console.log(str);
```

## Options

### outputMonthNames

Default: `false`

```ts
const arr = [[1], [1], [1], [1, 2, 3], [1, 2, 3]];
const str = arrayToString(arr, { outputMonthNames: true });

// Prints: '1 1 1 JAN-MAR 1-3'
console.log(str);
```

### outputWeekdayNames

Default: `false`

```ts
const arr = [[1], [1], [1], [1, 2, 3], [1, 2, 3]];
const str = arrayToString(arr, { outputWeekdayNames: true });

// Prints: '1 1 1 1-3 MON-WED'
console.log(str);
```

### outputHashes

Default: `false`

```ts
const arr = [[1], [1], [1], [1, 6, 11], [0, 1, 2, 3, 4, 5, 6]];

// Prints: '1 1 1 H/5 H'
console.log(arrayToString(arr, { outputHashes: true }));
```

### enableLastDayOfMonth

Default: `true`

```ts
const arr = [[1], [1], [1], [-1], [1]];

// Prints: '1 1 1 L 1'
console.log(arrayToString(arr, { enableLastDayOfMonth: true }));
```

```ts
const str = '1 1 1 L 1';

// Prints: [[1], [1], [1], [-1], [1]]
console.log(stringToArray(str, { enableLastDayOfMonth: true }));
```

## Get the schedule execution times

```ts
// Convert a string to an array
const arr = stringToArray("*/5 * * * *");

// Get the iterator, initialised to now
let schedule = getSchedule(arr);

// Optionally pass a reference `Date` and a `timezone`
let reference = new Date(2013, 2, 8, 9, 32);
schedule = getSchedule(arr, reference, "Europe/London");

// Calls to `.next()` and `.prev()` return a Luxon `DateTime` object

// Prints: '2013-03-08T09:35:00+00:00''
console.log(schedule.next().format());
// Prints: '2013-03-08T09:40:00+00:00''
console.log(schedule.next().format());

// Reset
schedule.reset();

// Prints: '2013-03-08T09:30:00+00:00''
console.log(schedule.prev().format());
// Prints: '2013-03-08T09:25:00+00:00''
console.log(schedule.prev().format());
```

## Get the units configuration
This is useful if you are creating a user interface. See [units.ts](./src/units.ts).
```ts
const units = getUnits();
```

## Test and build

```bash
git clone https://github.com/roccivic/cron-converter
cd cron-converter
yarn
yarn build
yarn test
yarn coverage
```

---
_Source: https://npm.io/package/cron-converter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
