Javascript Cron Expression from Certain Date with Frequency

I am trying to write a cron expression starting from a Certain Date and make it with certain frequency: run weekly, monthly, or annually?

Reading through cron expression, not sure how it can be expressed from Javascript code .

https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm

enter image description here

Currently using Typescript in Angular 10,

20 thoughts on “Javascript Cron Expression from Certain Date with Frequency”

  1. Here is Typescript code prepared:

      createweeklyExpression(startDate: Date): string {
        const minute = startDate.getMinutes();
        const hour = startDate.getHours();  
        const weekday = startDate.getDay();
    
        const finalValue = `${minute} ${hour} * * ${weekday}`;
        return finalValue;
      }
    
      createMonthlyExpression(startDate: Date): string {
        const minute = startDate.getMinutes();
        const hour = startDate.getHours();
        const dayOfMonth = startDate.getDate();
    
        const finalValue = `${minute} ${hour} ${dayOfMonth} * *`;
        return finalValue;
      }
    
      createAnnualExpression(startDate: Date): string {
        const minute = startDate.getMinutes();
        const hour = startDate.getHours();
        const dayOfMonth = startDate.getDate();
        const month = startDate.getMonth();
    
        const finalValue = `${minute} ${hour} ${dayOfMonth} ${month} *`;
        return finalValue;
      }
    
    Reply

Leave a Comment