admin管理员组

文章数量:1279118

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?

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?

Share Improve this question edited Apr 25, 2022 at 10:08 Sebastiaan asked Apr 25, 2022 at 10:03 SebastiaanSebastiaan 811 gold badge1 silver badge3 bronze badges 2
  • stackoverflow./questions/36263810/… – 0stone0 Commented Apr 25, 2022 at 10:05
  • @0stone0 I've seen cronstrue, but it returns the long variant of the English string. I'm looking for a shorter version as described above – Sebastiaan Commented Apr 25, 2022 at 10:11
Add a ment  | 

1 Answer 1

Reset to default 9

You can use https://www.npmjs./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"

本文标签: javascriptPackage to translate a cron to a quotsimplerquot human readable formatStack Overflow