• Stars
    star
    994
  • Rank 46,081 (Top 1.0 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript library that translates Cron expressions into human readable descriptions

cRonstrue Build Status NPM Package


 Would you take a quick second and ⭐️ my repo?


cRonstrue is a JavaScript library that parses a cron expression and outputs a human readable description of the cron schedule. For example, given the expression "*/5 * * * *" it will output "Every 5 minutes".

This library was ported from the original C# implementation called cron-expression-descriptor and is also available in a few other languages.

Features

  • Zero dependencies
  • Supports all cron expression special characters including * / , - ? L W, #
  • Supports 5, 6 (w/ seconds or year), or 7 (w/ seconds and year) part cron expressions
  • Supports Quartz Job Scheduler cron expressions
  • i18n support with 30+ languages

Demo

A demo is available here.

Installation

cRonstrue is exported as an UMD module so it will work in an AMD, CommonJS or browser global context.

First, install the module:

npm install cronstrue

Then, depending upon your usage context, add a reference to it:

Node / CommonJS

const cronstrue = require('cronstrue');

ESM / webpack / TypeScript

import cronstrue from 'cronstrue';

Browser

The cronstrue.min.js file from the /dist folder in the npm package should be served to the browser. There are no dependencies so you can simply include the library in a <script> tag.

<script src="cronstrue.min.js" type="text/javascript"></script>
<script>
  var cronstrue = window.cronstrue;
</script>

CDN

A simple way to load the library in a browser is by using the unpkg CDN, which is a "fast, global content delivery network for everything on npm". To use it, include a script tag like this in your file:

<script src="https://unpkg.com/cronstrue@latest/dist/cronstrue.min.js" async></script>

Using the "latest" tag will result in a 302 redirect to the latest version tag so it is recommended to use a specific version tag such as https://unpkg.com/[email protected]/dist/cronstrue.min.js to avoid this redirect.

Usage

cronstrue.toString("* * * * *");
> "Every minute"

cronstrue.toString("0 23 ? * MON-FRI");
> "At 11:00 PM, Monday through Friday"

cronstrue.toString("0 23 * * *", { verbose: true });
> "At 11:00 PM, every day"

cronstrue.toString("23 12 * * SUN#2");
> "At 12:23 PM, on the second Sunday of the month"

cronstrue.toString("23 14 * * SUN#2", { use24HourTimeFormat: true });
> "At 14:23, on the second Sunday of the month"

cronstrue.toString("* * * ? * 2-6/2", { dayOfWeekStartIndexZero: false });
> "Every second, every 2 days of the week, Monday through Friday"

cronstrue.toString("* * * 6-8 *", { monthStartIndexZero: true });
> "Every minute, July through September"

For more usage examples, including a demonstration of how cRonstrue can handle some very complex cron expressions, you can reference the unit tests.

CLI Usage

$ npm install -g cronstrue

$ cronstrue 1 2 3 4 5
At 02:01 AM, on day 3 of the month, and on Friday, only in April

$ cronstrue 1 2 3
Error: too few arguments (3): 1 2 3
Usage (5 args): cronstrue minute hour day-of-month month day-of-week
or
Usage (6 args): cronstrue second minute hour day-of-month month day-of-week
or
Usage (7 args): cronstrue second minute hour day-of-month month day-of-week year

Options

An options object can be passed as the second parameter to cronstrue.toString. The following options are available:

  • throwExceptionOnParseError: boolean - If exception occurs when trying to parse expression and generate description, whether to throw or catch and output the Exception message as the description. (Default: true)
  • verbose: boolean - Whether to use a verbose description (Default: false)
  • dayOfWeekStartIndexZero: boolean - Whether to interpret cron expression DOW 1 as Sunday or Monday. (Default: true)
  • monthStartIndexZero: boolean - Wether to interpret January as 0 or 1. (Default: false)
  • use24HourTimeFormat: boolean - If true, descriptions will use a 24-hour clock (Default: false but some translations will default to true)
  • locale: string - The locale to use (Default: "en")

i18n

To use the i18n support cRonstrue provides, you can either import all the supported locales at once (using cronstrue/i18n) or import individual locales (using cronstrue/locales/[locale]). Then, when calling toString you pass in the name of the locale you want to use. For example, for the es (Spanish) locale, you would use: cronstrue.toString("* * * * *", { locale: "es" }).

All Locales

You can import all locales at once with cronstrue/i18n. This approach has the advantage of only having to load one module and having access to all supported locales. The tradeoff with this approach is a larger module (~130k, minified) that will take longer to load, particularly when sending down to a browser.

// Node / CommonJS
const cronstrue = require('cronstrue/i18n');

// ESM / webpack / TypeScript
import cronstrue from 'cronstrue/i18n';

// Browser
<script src="https://unpkg.com/cronstrue@latest/cronstrue-i18n.min.js" async></script>

cronstrue.toString("*/5 * * * *", { locale: "fr" }); // => Toutes les 5 minutes
cronstrue.toString("*/5 * * * *", { locale: "es" }); // => Cada 5 minutos

Individual Locales

You can also load the main cronstrue module and then load individual locale modules you want to have access to. This works well when you have one or more locales you know you need access to and want to minimize load time, particularly when sending down to a browser. The main cronstrue module is about 42k (minified) and each locale is about 4k (minified) in size.

// Node / CommonJS
const cronstrue = require('cronstrue');
require('cronstrue/locales/fr');
require('cronstrue/locales/es');

// ESM / webpack / TypeScript
import cronstrue from 'cronstrue';
import 'cronstrue/locales/fr';
import 'cronstrue/locales/es';

// Browser
<script src="https://unpkg.com/cronstrue@latest/dist/cronstrue.min.js" async></script>
<script src="https://unpkg.com/cronstrue@latest/locales/fr.min.js" async></script>
<script src="https://unpkg.com/cronstrue@latest/locales/es.min.js" async></script>

cronstrue.toString("*/5 * * * *", { locale: "fr" }); // => Toutes les 5 minutes
cronstrue.toString("*/5 * * * *", { locale: "es" }); // => Cada 5 minutos

Frequently Asked Questions

  1. The cron expression I am passing in is not valid and this library is giving strange output. What should I do?

    This library does not do full validation of cron expressions and assumes the expression passed in is valid. If you need to validate an expression consider using a library like cron-parser. Example validation with cron-parser:

    const cronParser = require("cron-parser");
    const cronstrue = require("cronstrue");
    
    const expression = "* * * * * *";
    
    // Validate expression first
    let isCronValid = true;
    try { cronParser.parseExpression(expression) } catch(e) { isCronValid = false; }
    
    // If valid, then pass into cRonstrue
    if (isCronValid) {
      console.log(cronstrue.toString("* * * * *"));
    }
    
  2. Can cRonstrue output the next occurrence of the cron expression?

    No, cRonstrue does not support this. This library simply describes a cron expression that is passed in.

Supported Locales

License

cRonstrue is freely distributable under the terms of the MIT license.

More Repositories

1

cron-expression-descriptor

A .NET library that converts cron expressions into human readable descriptions.
C#
903
star
2

aspnet-core-react-template

ASP.NET Core 3.1 / React SPA Template App
C#
620
star
3

aspnet-core-vuejs-template

ASP.NET Core / Vue.js SPA Template App
C#
79
star
4

ansible-rails

Ansible playbook for provisioning and deploying a Rails/MySQL app to an Ubuntu server
Jinja
72
star
5

koa-vuejs-template

Koa / Vue.js SPA Template App
TypeScript
53
star
6

smartthings-rest-api

SmartThings REST API
Groovy
51
star
7

dbup-sqlserver-scripting

SQL Server object definition scripting for DbUp
C#
44
star
8

vscode-pgFormatter

A VS Code extension that formats PostgresSQL SQL
TypeScript
41
star
9

dotfiles

My system configuration for macOS, consisting of config dotfiles and shell scripts
Vim Script
38
star
10

jquery-googleslides

A jQuery plugin to display your Google Photos.
HTML
23
star
11

jBash

Helpers for Bash like shell scripting in JavaScript
JavaScript
21
star
12

jsh

Helpers for Bash like shell scripting in JavaScript
TypeScript
15
star
13

tiger-provisioning

Ansible provisioning for the Raspberry Pi running ArchLinux ARM sitting in my closet
Shell
12
star
14

dbup-consolescripts

Package Manager Console scripts for DbUp
PowerShell
11
star
15

npm-github-release

Automates the full release process for npm packages
JavaScript
11
star
16

debt-paydown-calculator

Compare methods to payoff your debt
TypeScript
10
star
17

vuejs-test-mocha-typescript-example

Example unit testing vue-class-component Vue.js components with Mocha and TypeScript
JavaScript
9
star
18

openbank

A friendly REST service wrapper for OFX bank servers.
JavaScript
9
star
19

postgraphile-playground

A web app using PostGraphile
JavaScript
8
star
20

raspberrypi-relay-controller

An Ansible playbook to provision a Raspberry Pi as a 2-channel relay control server
Groovy
8
star
21

pedamorf

A PDF conversion server for Windows that supports documents, text, images, html and urls.
C#
8
star
22

vistaicm-server

A Node.js app that works with the VISTA-ICM module to monitor and control a Honeywell VISTA alarm panel.
HTML
8
star
23

veritas

A web application for social groups to share contact info, calendar events, track attendance and more.
Ruby
7
star
24

ynab-utils

Some miscellaneous utilities for YNAB
TypeScript
6
star
25

picasawebsync

Synchronizes local photos and videos to online Picasa Web Albums
C#
6
star
26

test-microphone

A simple web app to test your microphone
HTML
6
star
27

sveltekit-auth-template

TypeScript
5
star
28

bank-alerts-to-ynab

This project routes spending alert emails from your bank to YNAB so your transactions appear in YNAB seconds after they occur.
JavaScript
5
star
29

psqlformat

A PostgreSQL SQL syntax formatter
TypeScript
5
star
30

franz-recipe-basecamp

Basecamp recipe for Franz
JavaScript
5
star
31

geekytidbits.com

My Geeky Blog
Handlebars
4
star
32

ynab-api-webhooks

Webhooks for the YNAB API
TypeScript
4
star
33

vue-typescript-bulma-template

A Vue.js template using TypeScript and Bulma
JavaScript
4
star
34

vscode-debugging-ts-code

Demo project showing how to debug TypeScript in VSCode using ts-node
TypeScript
3
star
35

docker-postgresql-northwind

A PostgreSQL server with the Northwind sample database
TSQL
3
star
36

ts-progressive-convert-namespace-modules

Example of progression conversion from TypeScript namespaces to modules
TypeScript
3
star
37

the-painting-app

A painting app my 6 year-old daughter and I made together
HTML
2
star
38

xertz

A static site generator written in TypeScript
TypeScript
2
star
39

covid19-chart

A COVID-19 D3.js line chart comparing cases between 2 countries
HTML
2
star
40

treasure-castle

A simple text based game used as a teaching tool for my daughter and her friend Noah
JavaScript
2
star
41

google-action-tiger

A Google Assistant Action (with an invocation name of "tiger") which controls a Honeywell VISTA-20P alarm panel and opens/closes garage doors
JavaScript
2
star
42

atx-node-typescript

Examples for 'A Fresh Look at TypeScript' (Austin Node.js; 10/16/2019)
2
star
43

cos-js-typescript

Examples for 'A Fresh Look at TypeScript' (coloradoSprings.js; 05/26/2021)
1
star
44

rabbitmq-email

A RabbitMQ Docker image that has SMTP support for incoming and outgoing email
Dockerfile
1
star
45

syndication-fetcher

A RSS and Atom feed parser
TypeScript
1
star
46

auto-import-ynab-transactions

Setup a recurring scheduled job to automatically import your YNAB transactions
1
star
47

bento-budget-app

Bento Budget - Envelope based budgeting web application
Ruby
1
star
48

home-assistant-config

My configuration for Home Assistant
1
star
49

franz-recipe-github-notifications

Basecamp recipe for GitHub Notifications
JavaScript
1
star
50

vuejs-playground

A playground for learning Vue.js
JavaScript
1
star
51

alexa-guitar-ace

An Alexa Skill that helps you tune and plays chords on your guitar
JavaScript
1
star