Package to translate a cron to a "simpler" human readable format?
Asked Answered
B

1

8

I've been looking all over the internet for an NPM package that could achieve this, but I haven't been able to find one.

What I'm looking for is quite simple on the surface. A cron library that can translate a monthly cron job to human-readable text, while keeping it simple. So for example: if I put in 0 0 14 * *, I want it to translate to "Monthly". Instead, this translates to "At 00:00 on day-of-month 14." which is an awkward string to display to users.

More examples would be:

  • 0 8 * * * should translate to "Daily"
  • 0 0 10 * * should also translate to "Monthly"

I want to stay away from making my own proprietary library if possible. Does anyone know if there is any JS package out there that meets my requirements?

Biramous answered 25/4, 2022 at 10:3 Comment(2)
#36264310Confiscatory
@Confiscatory I've seen cronstrue, but it returns the long variant of the English string. I'm looking for a shorter version as described aboveBiramous
D
6

You can use https://www.npmjs.com/package/cronstrue

cRonstrue is a JavaScript library that parses a cron expression and outputs a human readable description of the cron schedule.

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"
Dynamiter answered 30/11, 2022 at 7:7 Comment(2)
That's an awesome package – thanks! I've used to develop this utility ray.run/tools/cron-parserBirr
@LucGagan ImpressiveDynamiter

© 2022 - 2024 — McMap. All rights reserved.