Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Last active June 7, 2022 11:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidWells/2a25ad4689437f3cbfa0a0b855af7041 to your computer and use it in GitHub Desktop.
Save DavidWells/2a25ad4689437f3cbfa0a0b855af7041 to your computer and use it in GitHub Desktop.
Cron utilities
const cronstrue = require('cronstrue')
// https://github.com/harrisiirak/cron-parser
const MONTHS = [null, 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
const DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
const dayNum = {
sun: '0',
mon: '1',
tue: '2',
wed: '3',
thu: '4',
fri: '5',
sat: '6'
}
const monthNum = {
jan: '1',
feb: '2',
mar: '3',
apr: '4',
may: '5',
jun: '6',
jul: '7',
aug: '8',
sep: '9',
oct: '10',
nov: '11',
dec: '12'
}
const cronValues = {
'every-minute' : '* * * * *',
'every-1-minute' : '* * * * *',
'every-2-minutes' : '*/2 * * * *',
'every-even-minute' : '*/2 * * * *',
'every-uneven-minute' : '1-59/2 * * * *',
'every-3-minutes' : '*/3 * * * *',
'every-4-minutes' : '*/4 * * * *',
'every-5-minutes' : '*/5 * * * *',
'every-five-minutes' : '*/5 * * * *',
'every-6-minutes' : '*/6 * * * *',
'every-10-minutes' : '*/10 * * * *',
'every-ten-minutes' : '*/10 * * * *',
'every-15-minutes' : '*/15 * * * *',
'every-fifteen-minutes' : '*/15 * * * *',
'every-quarter-hour' : '*/15 * * * *',
'every-20-minutes' : '*/20 * * * *',
'every-30-minutes' : '*/30 * * * *',
'every-hour-at-30-minutes' : '30 * * * *',
'every-half-hour' : '*/30 * * * *',
'every-60-minutes' : '0 * * * *',
'every-hour' : '0 * * * *',
'every-1-hour' : '0 * * * *',
'every-2-hours' : '0 */2 * * *',
'every-two-hours' : '0 */2 * * *',
'every-even-hour' : '0 */2 * * *',
'every-other-hour' : '0 */2 * * *',
'every-3-hours' : '0 */3 * * *',
'every-three-hours' : '0 */3 * * *',
'every-4-hours' : '0 */4 * * *',
'every-6-hours' : '0 */6 * * *',
'every-six-hours' : '0 */6 * * *',
'every-8-hours' : '0 */8 * * *',
'every-12-hours' : '0 */12 * * *',
'hour-range' : '0 9-17 * * *',
'between-certain-hours' : '0 9-17 * * *',
'every-day' : '0 0 * * *',
'daily' : '0 0 * * *',
'once-a-day' : '0 0 * * *',
'every-night' : '0 0 * * *',
'every-day-at-1am' : '0 1 * * *',
'every-day-at-2am' : '0 2 * * *',
'every-day-8am' : '0 8 * * *',
'every-morning' : '0 9 * * *',
'every-midnight' : '0 0 * * *',
'every-day-at-midnight' : '0 0 * * *',
'every-night-at-midnight' : '0 0 * * *',
'every-sunday' : '0 0 * * SUN',
'every-monday' : '0 0 * * MON',
'every-tuesday' : '0 0 * * TUE',
'every-wednesday' : '0 0 * * WED',
'every-thursday' : '0 0 * * THU',
'every-friday' : '0 0 * * FRI',
'every-friday-at-midnight' : '0 0 * * FRI',
'every-saturday' : '0 0 * * SAT',
'every-weekday' : '0 0 * * 1-5',
'weekdays-only' : '0 0 * * 1-5',
'monday-to-friday' : '0 0 * * 1-5',
'every-weekend' : '0 0 * * 6,0',
'weekends-only' : '0 0 * * 6,0',
'every-7-days' : '0 0 * * 0',
'weekly' : '0 0 * * 0',
'once-a-week' : '0 0 * * 0',
'every-week' : '0 0 * * 0',
'every-month' : '0 0 1 * *',
'monthly' : '0 0 1 * *',
'once-a-month' : '0 0 1 * *',
'every-other-month' : '0 0 1 */2 *',
'every-quarter' : '0 0 1 */3 *',
'every-6-months' : '0 0 1 */6 *',
'every-year' : '0 0 1 1 *'
}
const cronAliases = {
'@yearly': cronValues['every-year'],
'@annually': cronValues['every-year'],
'@monthly': cronValues.monthly,
'@weekly': cronValues.weekly,
'@daily': cronValues.daily,
'@midnight': cronValues.daily,
'@hourly': cronValues['every-hour']
}
const suffixes = ['th', 'st', 'nd', 'rd']
function ordinal(num) {
const val = num % 100
return num + (suffixes[(val - 20)%10] || suffixes[val] || suffixes[0])
}
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"

Cron Syntax

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7, 1L - 7L) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31, L)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, optional)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment