Last active
March 13, 2023 16:48
-
-
Save iKunalChhabra/b72f4b99f5b950d7ec8f506ea7bcbc61 to your computer and use it in GitHub Desktop.
parse cron expression in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cronstrue = require("cronstrue"); // https://www.npmjs.com/package/cronstrue | |
const cronparser = require("cron-parser"); // https://www.npmjs.com/package/cron-parser | |
const timeDiff = require("js-time-diff"); // https://www.npmjs.com/package/js-time-diff | |
let cronExpression = "5 * * * SUN"; | |
let currentDate = new Date(); | |
try { | |
let nextDate = cronparser.parseExpression(cronExpression).next().toDate(); | |
let cronMeaning = cronstrue.toString(cronExpression); | |
console.log("Cron expression: " + cronExpression); | |
console.log("The job will run " + cronMeaning.toLowerCase()); | |
console.log("Current time is " + currentDate.toLocaleString("en-US")); | |
console.log( | |
"Next run will happen at " + | |
nextDate.toLocaleString("en-US") + | |
" i.e about " + | |
timeDiff(nextDate, currentDate) | |
); | |
} catch (err) { | |
console.log("Invalid cron expression"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment