MarkAllen4512
MarkAllen4512

Reputation: 139

Get Cron Expression from Start Dates, Weekly, Monthly, and Annually

Is there way in Typescript to derive Cron expressions from a start date, for Weekly, Monthly, and Annually. I have been searching for existing code base.

Upvotes: 1

Views: 422

Answers (1)

MarkAllen4512
MarkAllen4512

Reputation: 139

  getCreateWeeklyFromStartDate(startDate: Date): string {
    const startWeekday = startDate.getDay();
    const startHour = startDate.getHours();
    const startMinute = startDate.getMinutes();
    const cronExpression = `${startMinute} ${startHour} * * ${startWeekday}`;
    return cronExpression; 
  }

  getCreateMonthlyFromStartDate(startDate: Date): string {
    const startDayOfMonth = startDate.getDate();
    const startHour = startDate.getHours();
    const startMinute = startDate.getMinutes();
    const cronExpression = `${startMinute} ${startHour} ${startDayOfMonth} * *`;
    return cronExpression;
  }

  getCreateAnnualFromStartDate(startDate: Date): string {
    const startMonth = startDate.getMonth() + 1;
    const startDayOfMonth = startDate.getDate();
    const startHour = startDate.getHours();
    const startMinute = startDate.getMinutes();
    const cronExpression = `${startMinute} ${startHour} ${startDayOfMonth} ${startMonth} *`;
    return cronExpression;
  }

Upvotes: 1

Related Questions