Generate a PDF once every month (cron expr)

I want to use services to generate a PDF once every month. The date is dependent on user input. For example, one user can write in a text field that it wants the PDF every month on the 6th.

How can I build a cron expression based on the input? As I see it now, cron expressions can only be hard coded.

Thanks :slight_smile:

Hi @SinanMaric, this is an interesting question that touches both on vanilla JavaScript and on Create specifics :slight_smile: There are surely several ways to achieve it and the best implementation will depend on how much flexibility your users will get when setting up their preferences.

Assuming that you store information about when a PDF should be generated as a datetime property, you could have a service running daily and checking if the PDF generation date === Date Today. This way you work your way around cron expressions altogether.

An alternative would be converting a Date object into a cron expression with a JS function which could look something like that:

const dateToCron = (date) => {
    const minutes = date.getMinutes();
    const hours = date.getHours();
    const days = date.getDate();
    const months = date.getMonth() + 1;
    const dayOfWeek = date.getDay();

    return `${minutes} ${hours} ${days} ${months} ${dayOfWeek}`;

const date = new Date();

const cron = dateToCron(date);
console.log(cron); //'4 14 7 3 2'

Hope this helps!