Translate Cron Expressions Into Human-readable Strings – cRonstrue

Category: Javascript | April 2, 2024
Author:bradymholt
Views Total:5 views
Official Page:Go to website
Last Update:April 2, 2024
License:MIT

Preview:

Translate Cron Expressions Into Human-readable Strings – cRonstrue

Description:

A Cron expression is a text format specifying the schedule for automated jobs. It uses numbers and special characters to define the schedule, which can be difficult for non-technical users to understand.

The cRonstrue.js JavaScript library provides a simple way to parse Cron expressions and generate human-readable strings of Cron job schedules. It recognizes all special characters used in Cron expressions, including * / , – ? L W, #, supports 5, 6 (with seconds or year), or 7 (with seconds and year) part cron expressions, and is compatible with Quartz Job Scheduler cron expressions.

It also supports internationalization with over 30 human languages, allowing you to output the Cron expression description in the user’s preferred language.

How to use it:

1. Install and import the cRonstrue.

# NPM
$ npm i cronstrue
// node.js
const cronstrue = require('cronstrue');
// ES module
import cronstrue from 'cronstrue';
// Browser
<script src="/dist/cronstrue.min.js"></script>
<script>
var cronstrue = window.cronstrue;
</script>

2. To translate a Cron expression, just call the toString() function, passing in your expression as an argument:

cronstrue.toString("1 2 3 4 5")
// => At 02:01 AM, on day 3 of the month, and on Friday, only in April
// CLI usage
$ cronstrue 1 2 3 4 5

3. Localize the human-readable descriptions:

  • en
  • af
  • ar
  • be
  • bg
  • ca
  • cs
  • es
  • da
  • de
  • fi
  • fr
  • fa
  • he
  • hu
  • it
  • id
  • ja
  • ko
  • my
  • nb
  • nl
  • pl
  • pt_BR
  • pt_PT
  • ro
  • ru
  • sk
  • sl
  • sw
  • sv
  • th
  • tr
  • uk
  • zh_CN
  • zh_TW
  • vi
// node.js
const cronstrue = require('cronstrue');
require('cronstrue/locales/es');
// ES module
import cronstrue from 'cronstrue';
import 'cronstrue/locales/es';
// Browser
<script src="/dist/cronstrue.min.js"></script>
<script src="/locales/es.min.js"></script>
cronstrue.toString("1 2 3 4 5", { 
  locale: "es" 
});

4. More configuration options.

cronstrue.toString("1 2 3 4 5", { 
  throwExceptionOnParseError: false,
  verbose: false,
  dayOfWeekStartIndexZero: true,
  monthStartIndexZero: false,
  use24HourTimeFormat: true,
  tzOffset: 0, // time offset (in hours) from UTC
});

Changelog:

v2.48.0 (04/02/2024)

  • fix local

v2.48.0 (02/05/2024)

  • fix: wrong word when DOM and DOW both present

v2.47.0 (12/02/2023)

  • Bugfixes

v2.44.0 (11/16/2023)

  • Add arabic option
  • Specify “cron” in error message

v2.43.0 (11/13/2023)

  • Fixed time formats for various locales

v2.42.0 (11/13/2023)

  • Ordering days on parse()

v2.41.0 (10/24/2023)

  • Add more debugging

v2.33.0 (10/23/2023)

  • feat: Added support for floating point tzOffset
  • Adjust day descriptor if time zone offset makes the day change
  • Script cleanup

v2.32.0 (09/07/2023)

  • feat: Add Bulgarian Locale

v2.31.0 (08/15/2023)

  • If string has a range in first interval don’t render “de”

v2.30.0 (08/14/2023)

  • Feature/tz
  • feat: Add Malay (Malaysia) Locale

v2.29.0 (08/09/2023)

  • add tzOffset

You Might Be Interested In:


Leave a Reply